未捕获的TypeError:无法读取null的属性“click”

时间:2017-09-09 21:01:41

标签: javascript

我试图点击类名为(xyz)的div

document.querySelector('.header-search-wrap').click();
<iframe src="https://www.gammerson.com" frameborder="0" width="500px" height="900px">
	</iframe>

所以我试着在下面运行 document.querySelector( 'XYZ'。)点击()。 但它给了我

error Uncaught TypeError: Cannot read property 'click' of null

但是当我去元素窗口并搜索XYZ类然后再次 返回到chrome的控制台窗口并再次在代码下面运行

document.querySelector('.xyz').click();

令人惊讶的是它有效。我不知道有什么问题可以帮我解决问题。我尝试了多次,它只在我在元素窗口中打开源代码然后运行代码时才有效。

2 个答案:

答案 0 :(得分:1)

如果元素是document.querySelector() 中的子元素,则无法通过查询iframe 等文档来选择元素。如果您愿意,则必须检索其documentJavascript - Get element from within an iFrame

但另一件事是你无法访问不属于同一来源的iframe嗯,我猜,你还没有指定),因为“同源政策”适用SecurityError: Blocked a frame with origin from accessing a cross-origin frame

关于你的身体:

  

打开控制台时,它会显示Uncaught TypeError:不能   读取属性'click'为null但是当你搜索所有   .header-search-wrap in elements tab然后再次执行   。document.querySelector( '标题搜索缠绕')点击();它会显示出来   你只是未定义这意味着它的工作

这是因为当您检查所述iframe中的元素时,DevTools'“上下文”(Chrome Dev Tools: <page context> and <top frame>?)更改为iframe并且您可以使用document.querySelector()自如。

答案 1 :(得分:0)

无论何时运行

    <?php include 'db.php';

    $GLOBALS["conn"] = conncet();

    if (isset($_POST['submit'])) {
        $ime = $_POST['ime'];
        $rm = $_POST['rm'];
        $minpl = $_POST['minpl'];
        $maxpl = $_POST['maxpl'];

        $query = "select * from zaposleni where ime = '{$ime}' and radno_mesto = '{$rm}' and plata between '{$minpl}' and '{$minpl}'";
        $result = mysqli_query($GLOBALS["conn"], $query);

        if (!$result) {
            echo "Query failed" . mysqli_error($GLOBALS["conn"]);
        }

        $count = mysqli_num_rows($result);

            if ($count == 0) {
                echo "NO RESULT";
            } else { 

        while($row = mysqli_fetch_assoc($result)) {
            $i = $row['ime'];
            $r = $row['radno_mesto'];
            $p = $row['plata'];

                ?>

                <table>
        <thead>
            <tr>
                <th>Ime:</th>
                <th>Radno mesto:</th>
                <th>Plata:</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td><?php echo $i; ?></td>
                <td><?php echo $r; ?></td>
                <td><?php echo $p; ?></td>
            </tr>
        </tbody>
    </table>


                <?php
            } }
        if ($ime == "" || $rm = "" || $minpl = "" || $maxpl = "") {
            echo "Morate uneti sva polja";
        }

            }


    ?> 


    <form action="" method="post">
        <div class="form-group">
            <label for="ime">Ime:</label>
            <input type="text" class="form-control" name="ime">
        </div>

        <div class="form-group">
            <label for="rm">Radno mesto:</label>
            <input type="text" class="form-control" name="rm">
        </div>

        <div class="form-group">
            <label for="minpl">MIN Plata:</label>
            <input type="text" class="form-control" name="minpl">
        </div>

        <div class="form-group">
            <label for="maxpl">MAX Plata:</label>
            <input type="text" class="form-control" name="maxpl">
        </div>

        <input type="submit" name="submit" value="Pretraga">
    </form>

如果它表示null表示 在那个时间点 ,则没有类xyz的元素,因此在null上调用click()会导致错误。