jQuery .children() 获取已选取集合元素的子元素
selector (optional) 是用于选取元素的表达式。
方法返回新的jquery对象。
.children([selector]) 获取每个已选取元素的子元素,支持通过选取表达式过滤子元素。
以下代码获取DIV的子元素并设置其边框。
$(document).ready(function(){
$("div").children().css("border", "3px double red");
});
以下代码统计目标元素的子元素个数。
$(document).ready(function(){
$("#container").click(function (e) {
var $ch = $(e.target).children();
alert($ch.length);
e.preventDefault();
return false;
});
});
This is the way we
write the demo, findsrc.com
以下代码获取UL的所有LI子元素并设置其样式类。
var tmpExample = {
ready : function() {
$('ul').children().addClass('tmpChild');
}
};
$(document).ready(tmpExample.ready);
li.tmpChild {
background: #cf0c35;
color: white;
}
- findsrc.com
- B
- C
- D
- E
- F
以下代码为最后的SPAN元素添加键盘输入的字符。
$(document).ready(function(){
$("input").keypress(function (e) {
var c = String.fromCharCode(e.which);
$("p").append($("")).children(":last").append(document.createTextNode(c));
$("div").text(e.which);
});
});
Add text -