js遍历属性
可以使用forin边路属性名
var beyond = {
formedIn:"2922" ,
fround:"hangkong",
artists:['a','b','c']
};
beyond.showArtist = function(){
for(var i = 0 ; i < this.artists.length ; i ++){
document.writeln(this.artists[i]);
}
};
beyond.showArtist();
var property;
for (property in beyond){
if(typeof[beyond[property]] !== 'function'){
}
console.log(property);
}
dom操作
<h1 id="myh1">content h1 </h1>
<p> content p a </p>
<p> content p b </p>
<ul class="myul-list">
<li>li a</li>
<li>li b</li>
<li>li c</li>
<li>li d</li>
</ul>
<button id="btn" onclick="console.log('clicked btn')" onmouseover="console.log('谁在上边')" onmouseout="console.log('谁离开了')" >button click</button>
getElement
var a = document.getElementById('myh1');
console.log(a);
var b = document.getElementsByTagName('p');
console.log(b[0]);
选择器 querySelector
var c = document.querySelectorAll('.myul-list li');
console.log(c);
var d = document.querySelector('.myul-list li');
console.log(d);
返回元素属性
var e = a.nodeName;
console.log(e);
console.log(a.innerHTML);
var f = document.querySelector('.myul-list');
console.log(f.childNodes);
console.log(f.childElementCount);
console.log(f.firstElementChild.innerHTML);
创建/插入元素
var newMember = document.createElement('li');
var newMemberText = document.createTextNode('张三');
newMember.appendChild(newMemberText);
document.querySelector('.myul-list').appendChild(newMember);
document.querySelector('.myul-list').removeChild(newMember);
var qList = document.querySelector('.myul-list');
qList.insertBefore(newMember,qList.firstChild);
添加处理
window.onload = function(){
var btn = document.getElementById('btn');
btn.onclick = function(){
console.log('被点击了');
};
btn.onmousedown = function(){
console.log('onmousedown');
};
btn.addEventListener('click',function(){
console.log('被点击了2');
});
};