有時候,一些很小的東西,去使用jquery之類的庫顯得太麻煩了,仍是用原生js來得簡潔。node
原生js獲取元素,並獲取元素的內部元素列表jquery
var li_list = window.document.getElementById('img_list').children;
for(var i =0; i < li_list.length ; i++){
var img_list = li_list[i].children;
for(var j=0;j<img_list.length;j++){
if(img_list[j].nodeName.toLowerCase() == 'img'){
img_list[j].addEventListener('click',function(){
document.getElementById("view").setAttribute('content','width=device-width, initial-scale=1, minimum-scale=1, user-scalable=yes');
document.getElementById('show_gallery').innerHTML = '<img src="' + this.getAttribute('big_img') + '" />';
document.getElementById('show_gallery').style.display = 'flex';flex
})
}
}
}this
原生js爲元素添加事件:scala
img_list[j].addEventListener('click',function(){
document.getElementById("view").setAttribute('content','width=device-width, initial-scale=1, minimum-scale=1, user-scalable=yes');
document.getElementById('show_gallery').innerHTML = '<img src="' + this.getAttribute('big_img') + '" />';
document.getElementById('show_gallery').style.display = 'flex';事件
}) get
//原生js操做元素樣式,改變元素內容it
document.getElementById('show_gallery').innerHTML = '<img src="' + this.getAttribute('big_img') + '" />';
document.getElementById('show_gallery').style.display = 'flex';io