jquery怎么获得动态添加后的子元素个数
实现思路:使用jQuery的length属性获取对象中元素的数目,$(selector).length。实例演示如下:1、HTML结构:设置id为test的ul下有4个列表元素
Glen
Tane
John
2、jquery代码:点击列表后获取li数量$(function(){ $("#test").click(function() { alert($("#test li").length); // 获取id为test的ul下的li元素的数量 });});jquery中怎样根据父级找元素
jquery中parent()可以获取父级元素,所以获得某元素父级的父级可以使用
$(selector).parent().parent();
示例如下
创建Html代码及css样式
class1
class2
class3
div{padding:10px 20px;border:4px solid #ebcbbe;}
div.class1{width:200px;height:120px;}
编写jquery代码
$(function(){
$("div.class3").click(function() {
obj = $(this).parent().parent();
alert(obj.prop('class'));
});
})
jQuery元素是否显示与隐藏的判断
JQuery元素否显示或者隐藏两种式主要通获取元素节点hiddenvisible两属性进行判断代码:varnode=$('#id');第种写if(node.is(':hidden')){ //node隐藏则显示node元素否则隐藏 node.show(); }else{ node.hide();}第二种写if(!node.is(':visible')){ //node隐藏则显示node元素否则隐藏 node.show(); }else{ node.hide();}if(node.is(':visible')){ //node显示则隐藏node元素否则显示 node.hide();}else{ node.show();}
发表评论