[英]jQuery selector targeting element with id AND class does not work
I have the following event-handling function:我有以下事件处理功能:
jQuery(document).on('click', '#button .submitb', function(e){alert();});
jQuery IS included in the html document. jQuery 包含在 html 文档中。 But, if I click on a <div id="button" class="submitb">Go!</div>
nothing happens.但是,如果我点击<div id="button" class="submitb">Go!</div>
没有任何反应。 I don't even get an error in the chrome-console.我什至没有在 chrome 控制台中收到错误消息。
.submitb
is not a descendant of #button
to target it with jquery just use the id `#button' .submitb
不是的后代#button
与jQuery的目标,它只是使用id`#键”
like this像这样
jQuery(document).on('click', '#button', function(e){alert();});
If you want to target a div id with that class aswell you can you just move the space.如果你想用那个类来定位一个 div id,你可以移动空间。
like this像这样
jQuery(document).on('click', '#button.submitb', function(e){alert();});
If you want to target the same element, then you cannot have a space
between selectors, that would mean .submitb
descendent of #button
.如果您要定位的相同元素,那么你就不能有space
选择之间,这将意味着.submitb
的后代#button
。
And since the element has ID you just actually need the id selector:并且由于元素具有 ID,您实际上只需要 id 选择器:
jQuery(document).on('click', '#button', function(e){alert();});
EDIT:编辑:
You cannot have multiple buttons with same ID !!您不能有多个具有相同 ID 的按钮! then you should use just the class
.那么你应该只使用class
。 Or give them different ID-ending like _0
, _1
and target with [id^=button]
或者给他们不同的 ID 结尾像_0
, _1
和目标[id^=button]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.