简体   繁体   English

使用document.getElementById找不到任何div

[英]Can't find any div using document.getElementById

I have a weird problem with my code, I'm changing the background color of some divs using Javascript, but "document.getElementById" is not finding the div and returns NULL. 我的代码有一个奇怪的问题,我正在使用Javascript更改某些div的背景颜色,但是“ document.getElementById”未找到div并返回NULL。

Javascript: Javascript:

function colorFill(id)
{
   console.log(id); // Shows the correct string (r1h1)
   var divID = document.getElementById(id);
   // Even if I replace the id by "r1h1", directly the name of the div, it shows NULL
   console.log(divID); // Shows NULL
}

The "id" is a string coming from a php function, that works, because when I try do alert(id) , it shows up correctly (r1h1), but when I try to find the div using that "id", it returns a NULL. “ id”是一个来自php函数的字符串,它可以工作,因为当我尝试执行alert(id) ,它会正确显示(r1h1),但是当我尝试使用该“ id”查找div时,它将返回NULL。 Also, I've tried to find other divs and it does the same. 此外,我试图找到其他div,它的作用相同。

Here's the div: 这是div:

HTML: HTML:

<?php
    include 'models/ms_model.php';
    if (isset( $_SESSION['gameState'] ) && !empty( $_SESSION['gameState'] ))
    { 
        fillGameTable();
    }
?>

<div id="row1" class="Row rowActive">
    <div id="r1h1" class="hole" style="float:left"></div>
</div>

I'm calling the Javascript function here: 我在这里调用Javascript函数:

PHP: PHP:

function fillGameTable()
{
    echo '<script src="views/js/ms.js"></script>';
    foreach($_SESSION['gameState'] as $ID)
    {
       echo'<script>colorFill("'.(string)$ID.'");</script>';
    }
}

The weirdest this is that I have another function finding that same div and it works, something is wrong with this function ... 最奇怪的是,我还有另一个函数可以找到相同的div,并且可以正常工作,此函数出了点问题...

You are outputting the colorFill() function before the divs in the document. 您将在文档中的div之前输出colorFill()函数。 You either have to switch that order, to make sure that the elements are all in place in the document before you call the colorFill() JS function, like this: 您必须切换顺序,以确保调用colorFill()JS函数之前 ,元素已在文档中全部就位,如下所示:

<div id="row1" class="Row rowActive">
    <div id="r1h1" class="hole" style="float:left"></div>
</div>
<?php
    include 'models/ms_model.php';
    if (isset( $_SESSION['gameState'] ) && !empty( $_SESSION['gameState'] )){ fillGameTable(); }
?>

Another option would be to refactor your code so that the fillGameTable() PHP function returns a javascript which is executed in the document ready event. 另一个选择是重构代码,以便fillGameTable()PHP函数返回一个在文档就绪事件中执行的javascript。

If you are calling the JavaScript function from PHP make sure you're quoting the argument for the function properly: 如果要从PHP调用JavaScript函数 ,请确保正确引用了该函数的参数:

function colorFill(id)
{
   var divID = document.getElementById(id);
   console.log(divID);
}

colorFill(r1h1); // not a string, returns null
colorFill('r1h1'); // string, returns object

It is likely you have something like this in PHP: PHP中可能有类似以下内容的东西:

echo "colorFill(" . $variable . ")";

What you need is this (note the inclusion of the quotes surrounding the argument): 您需要的是这个(注意在参数周围包括引号):

    echo "colorFill('" . $variable . "')";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM