如何利用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后的效果图:
jquery中获取元素宽度包含padding的方法是?
var paddingLeft = $("#contentDiv").css("padding-left"); // 结果:"5px"
paddingLeft.replace('px', '')); // 结果:"5"
parseInt(paddingLeft.replace('px', '')); //转换为int

