具有ID AND类的jQuery选择器定位元素不起作用 [英] 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包含在html文档中.但是,如果单击<div id="button" class="submitb">Go!</div>
,则不会发生任何事情.我什至在Chrome控制台上都没有收到错误.
jQuery IS included in the html document. But, if I click on a <div id="button" class="submitb">Go!</div>
nothing happens. I don't even get an error in the chrome-console.
推荐答案
如果要定位同一元素,则选择器之间不能有space
,这意味着.submitb
后裔 #button
.
If you want to target the same element, then you cannot have a space
between selectors, that would mean .submitb
descendent of #button
.
并且由于该元素具有ID ,因此您实际上只需要ID选择器:
And since the element has ID you just actually need the id selector:
jQuery(document).on('click', '#button', function(e){alert();});
您不能有多个具有相同ID的按钮!那么您应该只使用class
.或者给他们提供不同的ID结尾(例如_0
,_1
)并使用[id^=button]
You cannot have multiple buttons with same ID!! then you should use just the class
. Or give them different ID-ending like _0
, _1
and target with [id^=button]
这篇关于具有ID AND类的jQuery选择器定位元素不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!