程序猿阵线联盟-汇总各类技术干货让前端飞

js基础第六天

2017-11-25  本文已影响0人  LIT乐言

01-js对象

02-根据js对象动态设置属性

// 1.获取标签
var box = document.getElementById('box');

// 2.设置属性
//    box.style.width = '200px';
//    box.style.height = '200px';
//    box.style.background = 'red';
// 网络返回数据
var attr = {width:'200px',height:'200px',background:'yellow'};

// 动态设置
for(var k in attr){
    box.style[k] = attr[k];
}

03-json动态创建节点

04-设置节点的第二种方法

<div id="box" title="123" abcd="456"></div>
var box = document.getElementById('box');
// 预先绑定的属性
//    box.setAttribute('title','123');

// 绑定类名
//    box.className = 'box';
/*
第一个参数是要绑定的属性名称
第二个参数是要绑定属性值
* setAttribute()
* */
box.setAttribute('class','box');

// 获取属性
console.log(box.getAttribute('title'));
console.log(box.getAttribute('abcd'));

05-常用的方法抽取

function $(id) {
    return document.getElementById(id);
}
function getEleTagName(dom,tagName) {
    return dom.getElementsByTagName(tagName);
}
// 遍历dom
function each(dom,fn) {
    for(var i = 0; i<dom.length; i++){
//            console.log(btns[i].innerHTML);
        // 要做的事情我们并不知道,所以需要外界决定
        fn(dom[i],i);
    }
}
// 使用函数
each(btns,function (obj,i) {
    console.log(obj.innerHTML);
    console.log(i);
});

07-事件对象的认识

上一篇 下一篇

猜你喜欢

热点阅读