- appendChild()方法的使用:
作用是向節(jié)點末尾添加最后的一個子節(jié)點(或從一個元素移動到另外一個元素中);
var node=document.getElementById("myList2").lastChild;
document.getElementById("myList1").appendChild(node);
添加之前數(shù)據(jù)展示:
myList1: Coffee
Tea
myList2: Water
Milk
添加之后數(shù)據(jù)展示:
myList1: Coffee
Tea
Milk
myList2: Water
2.Object.values方法
該方法返回一個數(shù)組值朋,成員是參數(shù)對象自身的(不含繼承的)所有可遍歷( enumerable )屬性的鍵值洛波。
var obj = { foo: "bar", baz: 42 };
Object.values(obj)
//返回 ["bar", 42]
如果Object.values方法的參數(shù)是一個字符串,會返回各個字符組成的一個數(shù)組册舞。
Object.values('tmp')
// ['t', 'm', 'p']
Object.entries 與Object.values獲取到的值不同
該方法返回一個數(shù)組蕴掏,成員是參數(shù)對象自身的(不含繼承的)所有可遍歷( enumerable )屬性的鍵值對數(shù)組。(即獲取到所有的鍵值)
var obj = { foo: "bar", baz: 42 };
Object.values(obj)
//返回 ["foo", baz]
- forEach()遍歷數(shù)組的一種方式 與each()類似
var arr = [1,2,3,4];
arr.forEach(alert);
forEach()與each()的轉(zhuǎn)換:
forEach(function(vale,i,ary){
anything is ok
});
此方法可以轉(zhuǎn)化為:
$.each(function(i,value,ary){
anything is ok
})
4.push() 方法可向數(shù)組的末尾添加一個或多個元素,并返回新的長度盛杰。
該方法可把它的參數(shù)順序添加到 arrayObject 的尾部挽荡。它直接修改 arrayObject,而不是創(chuàng)建一個新的數(shù)組即供。push() 方法和 pop() 方法使用數(shù)組提供的先進后出棧的功能徐伐。
var ary = new Array(3)
ary[0] = "one"
ary[1] = "two"
ary[2] = "three"
document.write(ary + "<br />")
document.write(ary.push("two") + "<br />")
document.write(ary)
5.遍歷之map()
該方法是把每個元素通過函數(shù)傳遞到當前匹配集合中,生成包含返回值的新的 jQuery 對象募狂。
<form method="post" action="">
<fieldset>
<div>
<label for="two">2</label>
<input type="checkbox" value="2" id="two" name="number[]">
</div>
<div>
<label for="four">4</label>
<input type="checkbox" value="4" id="four" name="number[]">
</div>
<div>
<label for="six">6</label>
<input type="checkbox" value="6" id="six" name="number[]">
</div>
<div>
<label for="eight">8</label>
<input type="checkbox" value="8" id="eight" name="number[]">
</div>
</fieldset>
</form>
這個方法的返回結(jié)果是
$(':checkbox').map(function() {
return this.id;
}).get().join(',');