Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.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
Javascript jQuery无法识别按钮_Javascript_Jquery_Html - Fatal编程技术网

Javascript jQuery无法识别按钮

Javascript jQuery无法识别按钮,javascript,jquery,html,Javascript,Jquery,Html,我试图创建一个按钮的点击事件,但它不起作用 到目前为止,我得到的是: <script type="text/javascript"> $(document).ready(function() { $("#btn_showComments").click(function() { alert("Hi from the button! :)"); }); }); </script> <div id

我试图创建一个按钮的点击事件,但它不起作用

到目前为止,我得到的是:

<script type="text/javascript">
    $(document).ready(function() {
        $("#btn_showComments").click(function() {
            alert("Hi from the button! :)");
        });
    });
</script>

<div id="#background">
   <div id="#showMoreComments">
      <button id="#btn_showComments">Show more comments!</button>
   </div>
</div>

我知道这是个愚蠢的问题。但我做错了什么

从按钮id中删除。通知JQuery选择具有以下id的元素。 像这样:

<div id="#background">
   <div id="#showMoreComments">
      <button id="btn_showComments">Show more comments!</button>
   </div>
</div>
<script type="text/javascript">
    $(document).ready(function() {
        $("#btn_showComments").click(function() {
            alert("Hi from the button! :)");
        });
    });
</script>

从按钮id中删除。通知JQuery选择具有以下id的元素。 像这样:

<div id="#background">
   <div id="#showMoreComments">
      <button id="btn_showComments">Show more comments!</button>
   </div>
</div>
<script type="text/javascript">
    $(document).ready(function() {
        $("#btn_showComments").click(function() {
            alert("Hi from the button! :)");
        });
    });
</script>
应为-

<button id="btn_showComments">Show more comments!</button>
那么您的代码就可以正常工作了

请不要在ID的HTML标记中使用。附加以使用其ID获取元素

应为-

<button id="btn_showComments">Show more comments!</button>
那么您的代码就可以正常工作了


请不要在ID的HTML标记中使用。附加以使用其ID获取元素。

首先从按钮标记中的ID声明中删除,即:

<button id="btn_showComments">Show more comments!</button>

第二,将脚本移动到html下方。

首先从button标记中的id声明中删除,即:

<button id="btn_showComments">Show more comments!</button>
第二,将脚本移到html下面。

从html中的元素中删除

<div id="background">
   <div id="showMoreComments">
从html中的元素中删除

<div id="background">
   <div id="showMoreComments">

试试这段代码,实际上您已经添加了ID的html代码。请把它拿走

<script type="text/javascript">
    $(document).ready(function() {
        $("#btn_showComments").click(function() {
            alert("Hi from the button! :)");
        });
    });
</script>

<div id="background">
   <div id="showMoreComments">
      <button id="btn_showComments">Show more comments!</button>
   </div>
</div>

试试这段代码,实际上您已经添加了ID的html代码。请把它拿走

<script type="text/javascript">
    $(document).ready(function() {
        $("#btn_showComments").click(function() {
            alert("Hi from the button! :)");
        });
    });
</script>

<div id="background">
   <div id="showMoreComments">
      <button id="btn_showComments">Show more comments!</button>
   </div>
</div>


去掉div id=btn_showcoments>div上的标签,这应该是一条经验法则,不要让你的id以标签开头。html不是twitter@baao有些html很可能无法修改html,您可以通过使用\\转义来选择元素。选择器类似于$\\btn\u showComments。如果你能控制HTML,完全忽略这个注释;删除每个ID属性中的。去掉div ID=btn_showcoments>div上的,根据经验,它应该可以工作,不要让您的ID以标签开头。html不是twitter@baao有些html很可能无法修改html,您可以通过使用\\转义来选择元素。选择器类似于$\\btn\u showComments。如果你能控制HTML,完全忽略这个注释;在每个ID属性中删除。完全正确!!试着把原因贴出来,而不仅仅是一个工作代码。没错!!试着贴出原因,而不仅仅是一个工作代码。你的答案与其他四个有什么不同?先生,我同意你的观点,但我回复有点晚。你的答案与其他四个有什么不同?先生,我同意你的观点,但我回复有点晚。天哪!我为什么把它放在那里?我太笨了。我甚至没有意识到。哈哈哈,谢谢不要介意。这样的事情经常发生。祝你一切顺利天啊!我为什么把它放在那里?我太笨了。我甚至没有意识到。哈哈哈,谢谢不要介意。这样的事情经常发生。祝你一切顺利