简体   繁体   English

使用2 CSS类的Click函数在JQuery中不起作用

[英]using 2 css class for click function doesn't work in JQuery

I have to following code snippet. 我必须遵循以下代码段。 My understanding is that, the following code fires, when <li class="topics">Topics</li> is clicked on. 我的理解是,单击<li class="topics">Topics</li>时,将触发以下代码。 However, what I am observing is that, the click function doesn't fire. 但是,我观察到的是,单击功能不会触发。

   $(".li .topics").click(function () {

   }

However, if I am removing the .li , 但是,如果我要删除.li

   $(".topics").click(function () {

   }

Then the click function works fine, anything I am doing wrong here? 然后单击功能可以正常工作,这里我做错了什么吗? Please advise 请指教

li is not a class, it is an element. li不是类,而是元素。 what you can do is remove the period from the .li and have something like this: 您可以做的是从.li删除句点,并进行如下操作:

$("li.topics").click(function () {}

Also, when you have a space between the classes, it looks for the second class to be nested inside of the first, however, when you remove the space, it looks for that element which has that class. 同样,当类之间有空格时,它会寻找第二个类嵌套在第一个类的内部,但是,当您删除该空间时,它将寻找具有该类的元素。

If you have a class "li" then this should also work 如果您有班级“ li”,那么这也应该有效

$(".li, .topics").click(function(){
   alert("clicked");    
});​

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM