我有以下事件处理函数:
jQuery(document).on('click', '#button .submitb', function(e){alert();});
jQuery 包含在 html 文档中。但是,如果我点击 <div id="button" class="submitb">Go!</div>
什么都没发生。我什至没有在 chrome-console 中收到错误。
最佳答案
如果你想定位同一个元素,那么你不能在选择器之间有一个空格
,这意味着 .submitb
descendent of #按钮
。
因为元素有 ID,你实际上只需要 id 选择器:
jQuery(document).on('click', '#button', function(e){alert();});
编辑:
您不能有多个具有相同 ID 的按钮!!那么你应该只使用类
。或者给他们不同的 ID 结尾,例如 _0
、_1
并使用 [id^=button]
关于javascript - 具有 id 和类的 jQuery 选择器定位元素不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19346554/