Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php中的javascript代码无法访问html元素。document.getElementById不工作_Javascript_Php_Html - Fatal编程技术网

php中的javascript代码无法访问html元素。document.getElementById不工作

php中的javascript代码无法访问html元素。document.getElementById不工作,javascript,php,html,Javascript,Php,Html,php内部脚本中的document.getElementById(“n”)无法访问html中带有id=“n”的段落。 段落的内部html未被更改。 我也尝试使用jQuery,但它也无法更改innerHTML <?php $username="root"; $password=""; $servername="localhost"; $database="mydb"; $conn=mysqli_connect($servername,$username

php内部脚本中的
document.getElementById(“n”)
无法访问html中带有
id=“n”
的段落。 段落的内部html未被更改。 我也尝试使用jQuery,但它也无法更改
innerHTML

<?php
    $username="root";
    $password="";
    $servername="localhost";
    $database="mydb";
    $conn=mysqli_connect($servername,$username,$password,$database);
    if(!($conn)){
        die(mysqli_connect_error());
    }
    $sql="select passenger from place where source='chennai' and destination='bengaluru'";
    $result=mysqli_query($conn,$sql);
    if(mysqli_num_rows($result)>0){
        echo '<script>document.getElementById("n").innerHTML="Do Something";</script>';
    }
    mysqli_close($conn);
?>
<html>
    <head lang="en-us">
        <title>Cards</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <script src="js/materialize.min.js"></script>
        <script src="jquery-1.11.3.min.js"></script>
        <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
        <link rel="stylesheet" href="css/materialize.min.css">
        <link rel="stylesheet" href="demo_css.css">
    </head>    
    <body>
        <p id="n"></p>
        <div class="container" id="con">
            <div class="card hoverable">
                <div class="card-content waves-effect waves-block waves-light">
                    <h1 class="card-title activator grey-text text-darken-4" id="content"></h1>
                </div>
                <div class="card-reveal">
                    <span class="card-title grey-text text-darken-4">Passengers<i class="material-icons right">close</i></span>
                </div>
            </div>
        </div>
    </body>
</html>

document.getElementById('n')
不返回任何内容的原因是,它是在加载html的其余部分(包括您试图获取的元素)之前执行的

所以你有几个选择:

  • 将javascript放在html下面(最好在结束之前)。 这样,它将在元素加载到DOM中后执行

  • 使用加载DOM时激发的函数,并将js代码放在其中

  • 2.1。一种方法是使用jQuery的
    $(document.ready()

    2.2。在那篇文章中描述了一些常见的JS方法:

    这是因为php生成的js是在加载剩余时间之前执行的。如果您将它放在html块下面,它应该可以工作……如果您已经在使用jQuery,您可以使用
    $(document).ready()
    等待页面的其余部分加载并准备访问。或者使用一种普通的js方法,比如:javascript代码在哪里?这里只列出了php和html
    $(document).ready(function() {
        document.getElementById("n").innerHTML="Do Something";
    }