<jQuery-节点方法>

在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
</head>
<body>
	<div class="box">
		<p>hello0</p>
		<p><strong>hello1</strong></p>
		<p class="title">hello2</p>
		<h1>111</h1>
		<h2>222</h2>
		<p>hello3</p>
		<p>hello4</p>
	</div>
	<script src="./jquery-1.12.3/jquery-1.12.3.min.js"></script>
	<script type="text/javascript">
	//点击p获取内容
		$('p').click(function(){
			//this原生js对象
            console.log(this);
            //转换jQ对象
            console.log($(this));
            //建议获取内容使用jQ
            console.log($(this).html());
    //获取父节点,祖先节点
            console.log($(this).parent()[0]);//div
            console.log($(this).parents());//div>body>html
		    console.log($(this).parents('body'));//body
		});
    //点击div获取其第一层子节点
        $('.box').click(function(){
            console.log($(this).children());
        });
    //获取亲兄弟节点
        console.log($('.title').siblings());

    //获取某个节点后的节点
    	//获取1个
    	console.log($('.title').next());
    	//获取多个
    	console.log($('.title').nextAll());

    //获取某个节点之前的
    	//获取1个
    	console.log($('.title').prev());
    	//获取多个
    	console.log($('.title').prevAll());

    //查找后代子元素
    //继续查找将在strong内进行
    	console.log($('.box').find('strong'));
    	//查找失败
    	console.log($('.box').find('strong').find('h1'));
    //end方法返回上一个查找到的对象
    	console.log($('.box').find('h2').find('h1').end());
    //end对次使用,多次返回
    	console.log($('.box').find('h2').find('h1').end().end().find('h1'));
	</script>
</body>
</html>	
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值