jQuery给多个不同元素添加class样式的方法
jQuery可以通过addClass()方法给多个不同的html元素同时添加相同的class
<!DOCTYPE html>
<html>
<head>
<script src="js/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("h1,h2,p").addClass("blue");
$("div").addClass("important");
});
});
</script>
<style type="text/css">
.important
{
font-weight:bold;
font-size:xx-large;
}
.blue
{
color:blue;
}
</style>
</head>
<body>
<h1>Heading 1</h1>
<h2>Heading 2</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<div>This is some important text!</div>
<br>
<button>Add classes to elements</button>
</body>
</html>
js获取元素的方式
1 有多种方式可以获取元素,包括但不限于通过id、class、标签名、属性名等来获取元素。
2 具体来说,可以通过document.getElementById()、document.getElementsByClassName()、document.getElementsByTagName()、document.querySelector()、document.querySelectorAll()等方法来获取元素。
3 此外,还可以使用jQuery等库来获取元素,以及通过自定义属性、事件委托等方法来实现多样化的元素获取方式。
Jquery搜索父元素操作方法
使用js或者jquery查找父元素、子元素经常遇到。可是用起来总容易混淆,这里统一总结了一下,以后用起来相信会方便好多
这里jquery向上查找父元素 用到的方法:
closest() parents() parent()
向下查找子元素
用到的方法:find() children()
js用的是 children[] 属性 !
jQuery:如何取得当前元素的父元素的父元素
发现答非所问的人还不少啊
取父窗口的元素方法:$(selector, window.parent.document);
那么你取父窗口的父窗口的元素就可以用:$(selector, window.parent.parent.document);
类似的,取其它窗口的方法大同小异
$(selector, window.top.document);
$(selector, window.opener.document);
$(selector, window.top.frames[0].document);
希望对你能有帮助
还没有评论,来说两句吧...