[英]jQuery selector targeting element with id AND class does not work
我有以下事件处理功能:
jQuery(document).on('click', '#button .submitb', function(e){alert();});
jQuery 包含在 html 文档中。 但是,如果我点击<div id="button" class="submitb">Go!</div>
没有任何反应。 我什至没有在 chrome 控制台中收到错误消息。
.submitb
不是的后代#button
与jQuery的目标,它只是使用id`#键”
像这样
jQuery(document).on('click', '#button', function(e){alert();});
如果你想用那个类来定位一个 div id,你可以移动空间。
像这样
jQuery(document).on('click', '#button.submitb', function(e){alert();});
如果您要定位的相同元素,那么你就不能有space
选择之间,这将意味着.submitb
的后代#button
。
并且由于元素具有 ID,您实际上只需要 id 选择器:
jQuery(document).on('click', '#button', function(e){alert();});
编辑:
您不能有多个具有相同 ID 的按钮! 那么你应该只使用class
。 或者给他们不同的 ID 结尾像_0
, _1
和目标[id^=button]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.