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来向一个元素中添加和移除css类
Jquery使用addClass()与removeClass()来动态的添加或移出一个css类,例如:
1.$(“#para1”).addClass('highlight');添加一个“highlight”css类给id为para1的元素。
2.$(‘#para1’).removeClass(‘'highlight');从id为para1的元素中移出一个‘highlight’css类。具体实例代码如下:<html><head><styletype="text/css">.highlight{background:green;}</style><scripttype="text/javascript"src="jquery-1.
3.2.min.js"></script></head><body><h1>jQueryadd/removecssclassexample</h1><pid="para1">Thisisparagraph1</p><p>Thisisparagraph2</p><p>Thisisparagraph3</p><p>Thisisparagraph4</p><buttonid="addClass">Addhighlight</button><buttonid="removeClass">Removehighlight</button><scripttype="text/javascript">$("#addClass").click(function(){$('#para1').addClass('highlight');});$("#removeClass").click(function(){$('#para1').removeClass('highlight');});</script></body></html>初始的效果:点击addhighlight后的效果图:点击removehighlight后的效果图:
还没有评论,来说两句吧...