简体   繁体   English

为什么选择器的子节点不适用于jquery?

[英]Why children for a selector not works in jquery?

I am trying to change background color of li having a class with jquery children , but it is not working. 我试图改变li background color ,有一个带有jquery children的类,但它不起作用。 Following is the url where I test it : http://jsfiddle.net/kFUqK/1/ 以下是我测试它的网址: http//jsfiddle.net/kFUqK/1/

I changed the code 我改变了代码

$('ul.level-2').children().css('background-color', 'red');
$('ul.level-2').children('.level-33').css('background-color', 'blue');

to

//$('ul.level-2').children().css('background-color', 'red');
$('ul.level-2').children('.level-33').css('background-color', 'blue');

Then I found li.level-33 has not changed with its background. 然后我发现li.level-33没有改变它的背景。

Any reason for this? 有什么理由吗?

From the jQuery docs: 来自jQuery文档:

The .children() method differs from .find() in that .children() only travels a single level down the DOM tree while .find() can traverse down multiple levels to select descendant elements (grandchildren, etc.) as well. .children()方法不同于.find().children()只行进的单一电平向下DOM树而.find()可以向下遍历多个级别来选择后代元素(孙子,等)为好。

So you want find . 所以你想find

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

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