如何将变量php传递给javascript


How to pass variable php to javascript?

我想将变量从php传递给javascript,因此我创建了这样的代码

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="css/style.css">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.js"></script> 
</head>
<body>
<?PHP
 echo "<h2><a style='"font-family: 'Raleway', Arial, sans-serif'" class='"dummy-media-object'"><p id='"example2'"><font face='Raleway' size='"6'">http://examle.com</font></p>";
?>
<div id="article"></div>
<script type="text/javascript">

    $(document).ready(function(){
    $.ajax({
        type: "GET",
        url: document.getElementById("example2"),
        contentType: "application/json; charset=utf-8",
        async: false,
        dataType: "json",
        success: function (data, textStatus, jqXHR) {
        var markup = data.parse.text["*"];
        var i = $('<div></div>').html(markup);
        // remove links as they will not work
        i.find('a').each(function() { $(this).replaceWith($(this).html()); });
        // remove any references
        i.find('sup').remove();
        // remove cite error
        i.find('.mw-ext-cite-error').remove();
        $('#article').html($(i).find('p'));

        },
        error: function (errorMessage) {
        }
    });    
    });


</script>
<h1>
</h1>


</body>
</html>

在这里,document.getElementById("example2")将传递变量,但在这种情况下,这不会发生。我做错了什么?

它不起作用的原因是没有idexample2的DOM元素。有几种不同的方式可以发射此元素。

<?php echo "<span id='example2'>$somevalue</span> ?> // accessed by
document.getElementById("example2")

或者可以直接在javascript 中发出

<script type="text/javascript">
    <?php echo "var example2Value = $somevalue ?>
</script>

或者你可以创建一个隐藏的输入来保存你的变量

<?php echo "<input id='example2' type='hidden' value='$somevalue' /> ?> // accessed by
document.getElementById("example2").value

试试这个:-

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="css/style.css">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.js"></script> 
</head>
<body>
<?php echo "<h2><a style='"font-family: 'Raleway', Arial, sans-serif'" class='dummy-media-object'><font face='Raleway' size='6'><p id='example2'>http://examle.com</p></font></a></h2>";?> // change in formatting
<div id="article"></div>
<script type="text/javascript">

    $(document).ready(function(){
    $.ajax({
        type: "GET",
        url: document.getElementById("example2").innerHTML,
        contentType: "application/json; charset=utf-8",
        async: false,
        dataType: "json",
        success: function (data, textStatus, jqXHR) {
        var markup = data.parse.text["*"];
        var i = $('<div></div>').html(markup);
        // remove links as they will not work
        i.find('a').each(function() { $(this).replaceWith($(this).html()); });
        // remove any references
        i.find('sup').remove();
        // remove cite error
        i.find('.mw-ext-cite-error').remove();
        $('#article').html($(i).find('p'));

        },
        error: function (errorMessage) {
        }
    });    
    });
</script>
<h1>
</h1>
</body>
</html>

不需要echo和font标记。您使用jQuery,因此可以使用.text()方法访问它。

<h2 style="font-family: 'Raleway', Arial, sans-serif">
<a class="dummy-media-object"><p id="example2">http://examle.com</p></a>
<div id="article"></div>
<h1></h1>

使用此脚本-注意$("#example2").text()

我不相信您想要JSON,因为您将结果视为HTML如果example.com与脚本不是同一个服务器,则由于跨域访问违规,您将无法执行您想要的操作,除非您可以添加CORS

$(function () {
    $.get($("#example2").text(),function (data) {
      var $i = $('<div></div>').html(data);
      // remove links as they will not work
      $i.find('a').each(function () {
        $(this).replaceWith($(this).html());
      });
      // remove any references
      $i.find('sup').remove();
      // remove cite error
      $i.find('.mw-ext-cite-error').remove();
      $('#article').html($i.find('p'));
   });
});

要将php变量传递给javascript,只需执行以下操作:

<?php 
    $url = "http://examle.com"; 
?>
<script>
    var url = JSON.parse(<?php echo json_encode($url); ?>);
    alert(url) //Will alert "http://examle.com"
</script>