JS警报弹出时,id="东西"

JS alert pop up when id="something"

本文关键字:quot 东西 id JS      更新时间:2023-09-26

我有一些代码,它的ID是question-number,每次我点击我的按钮它就会上升数字。

我正试图写一个小脚本,将显示一个模型或警报框一旦id打印在页

" 40题2 "

我现在有

            <div class="columns">
                <div class="column-left">
                    <div id="question-number" class="question-text"></div>
                    <div id="question-time" class="question-time"></div>
                </div>
            </div>

what I have try

首次尝试

<script>
                function myFunction() {
                    var content =('#question-number').html();
                    if (content.indexOf("Question 2 of 40") != -1) {
                        alert("String Found");
                    }
                }
            </script>

第二次尝试

<script>
                    if($('#test-view').attr('question-number').indexOf('Question 2 of 40') != -1) {
                        alert('here !!!');
                    }
                </script>

有人知道为什么这两种尝试都不起作用吗?

更新

我只需要一个脚本说如果看到

40题2

on DOM的警告框

您的代码即将运行

您在第一行忘记了$

如果你只是声明函数而不调用它,它永远不会工作

var content = $('#question-number').html();
if (content.indexOf("Question 2 of 40") !== -1) {
  alert("String Found");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="columns">
  <div class="column-left">
    <div id="question-number" class="question-text">Question 2 of 40</div>
    <div id="question-time" class="question-time"></div>
  </div>
</div>

您的代码不知道何时运行。您需要添加以下内容:

$(function(){ //need this line to wait for document to be ready before binding to click event.
    $('#question-number').on('click', function(){
        //your code here
    }
});

<div id="question-number" class="question-text" onclick="myFunction()"></div>

我也会检查学习如何使用你的浏览器开发工具。当你遇到这样的问题时,他们会给你很大的帮助。