<!DOCTYPE?html>
<html>
<head>
????<meta?http-equiv="Content-type"?content="text/html;?charset=utf-8"?/>
????<title></title>
????<link?rel="stylesheet"?href="imooc.css"?type="text/css">
????<style>
????.left?{
????????width:?auto;
????????height:?200px;
????}
????
????.left?div?{
????????width:?150px;
????????height:?70px;
????????padding:?5px;
????????margin:?5px;
????????float:?left;
????????background:?#bbffaa;
????????border:?1px?solid?#ccc;
????}
????
????.span?{
????????color:?blue;
????}
????</style>
????<script?src="http://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js"></script>
</head>
<body>
????<h2>find方法()</h2>
????<div?class="left?first-div">
????????<div?class="div">
????????????<ul?class="level-1">
????????????????<li?class="item-1">1</li>
????????????????<li?class="item-2">2</li>
????????????????<li?class="item-3">3</li>
????????????</ul>
????????</div>
????????<div?class="div">
????????????<ul?class="level-2">
????????????????<li?class="item-1">1</li>
????????????????<li?class="item-2?test">2</li>
????????????????<li?class="item-3">3</li>
????????????</ul>
????????</div>
????????<div?class="div">
????????????<ul?class="level-3">
????????????????<li?class="item-1">1</li>
????????????????<li?class="item-2">2</li>
????????????????<li?class="item-3">3</li>
????????????</ul>
????????</div>
????</div>
????<button>點(diǎn)擊:find傳遞表達(dá)式</button>
????<br/>
????<br/>
????<h3>find表達(dá)式</h3>
????<div?style="border:1px?solid?red;">
????????<p>
????????????<span>測試1</span>
????????????<a>測試2</a>
????????</p>
????????<p>
????????????<span>慕課網(wǎng)1</span>
????????????<a>慕課網(wǎng)2</a>
????????</p>
????????<div>
????????????<span>Aaron1</span>
????????????<a>Aaron2</a>
????????</div>
????</div>
????<br/>
????<br/>
????<button>點(diǎn)擊:find傳遞$對象</button>
????<script?type="text/javascript">
????$("button:first").click(function()?{
????????$(".left").find("li:last").css('border','1px?solid?red')
????})
????</script>
????<script?type="text/javascript">
????$("button:last").click(function()?{
????????//找到所有p元素,然后篩選出子元素是span標(biāo)簽的節(jié)點(diǎn)
????????//改變其字體顏色
????????var?$spans?=?$('span');
????????$("p").find($spans).css('color',?'red');
????})
????</script>
</body>
</html>
為什么要寫上這一步:var?$spans?=?$('span');
2017-02-17
var?$spans?=?$('span'); ?這樣寫是找到全局的span。
然后再下一步?$("p").find($spans).css('color',?'red'); ? ? ? p中過濾掉 ?$spans ?
當(dāng)然,直接寫?$("p").find('span').css('color',?'red'); ?也可以達(dá)到同樣的效果
2017-06-15
我覺得那樣寫很麻煩 ,還不如直接寫$('p > span).css('color' , 'red');
2017-02-17
不寫也沒關(guān)系啊 , $(),是個對象 , 里面可以是element , 也可以是選擇器,或者其他的 , 不寫var?$spans?=?$('span');
把find()改成find('span')效果是一樣;不求甚解,別追究的太細(xì)了