js工作或不点击

问题描述 投票:-1回答:1

嘿家伙我需要JS onclick事件,但它对我来说非常奇怪,我无法弄清楚为什么。下面有三个例子,我不明白为什么第一个没有。

/首先

$(document).ready(function () {

$("button#clear-id").click(function(){      // this one doesn't work
    alert('Howdy');
});
...

/秒

$(document).ready(function () {

$("body").click(function(){      // this one works
    alert('Howdy');
});
...

/第三

$(document).ready(function () {

$(document).on('click', '#clear-id', function () {    // this one works
  alert("Hello!");
});
...

/按钮的HTML看起来像

<button type="button" class="close" id="clear-id" aria-label="Close" value="';   the_ID(); echo'">
    <span aria-hidden="true">&times;</span>
</button>
javascript javascript-events onclick
1个回答
-1
投票

显然第一个中的选择器是错误的,只使用$('#clear-id'),因为我不确定,但我认为你不能在同一个选择器中将标签与id结合起来。

© www.soinside.com 2019 - 2024. All rights reserved.