1.nth-of-type(n) 和 nth-child(n) 區別
<!DOCTYPE html>
<html>
<head>
<style>
p:nth-of-type(2)
{
background:#ff0000;
}
</style>
</head>
<body>
<div>
<h1>這是標題</h1>
<p>第一個段落。</p>
</div>
<div>
<p>第二個段落。</p>
<p>第三個段落。</p>
</div>
<p>第四個段落。</p>
<p>123123</p>
</body>
</html>
結果爲 '第三個段落' 和 '123123‘ 背景爲紅色。
而
<!DOCTYPE html>
<html>
<head>
<style>
p:nth-child(2)
{
background:#ff0000;
}
</style>
</head>
<body>
<div>
<h1>這是標題</h1>
<p>第一個段落。</p>
</div>
<div>
<p>第二個段落。</p>
<p>第三個段落。</p>
</div>
<p>第四個段落。</p>
<p>123123</p>
</body>
</html>
結果爲 '第一個段落“ 和 '第三個段落” 背景爲紅色。
重點:
p:nth-of-type: 選中的元素是父元素中所有p子元素的第二個。
p:nth-child:選中的元素爲p元素且爲父元素的第二個子元素。
2.深度克隆js對象的屬性
對於js中的數據,基本類型和引用類型的賦值時不同的。下面給出一個通用的js深度克隆方法。
function deepclone(obj) {
if (typeof(obj)!=='object' || obj===null) {
return obj;
}//所有的引用數據類型都指向object
var o,i,j;
if (obj instanceof Array){
o = [];
for (i = 0; i < obj.length; i++) {
if (typeof obj[i] ==='object' && obj[i]!= null) {
o[i] = deepclone(obj[i]);
}
else{
o[i] = obj[i];
}
}
}
else{
o = {};
for (j in obj) { //使用for in 進行對象屬性的遍歷
if (typeof(obj[j]==='object'&&obj[i]!==null)) {
o[i] = deepclone(obj[j]);
}
else{
o[j] = obj[j];
}
}
}
return o;
}