Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

js 操作 div.style.top和div.style['top'] 区别是什么

js 操作 div.style.top和div.style['top'] 区别是什么


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

在你这里的style里。"."和[]是没区别的

 console.log(div.style['top'] === div.style.top);

"."和[]的区别在于,请看你是怎样变成隔壁老王:

    var obj = {};
    obj.name = '隔壁老王';
    var yourName = "name";
    console.log(obj[yourName]);//隔壁老王
    console.log(obj.yourName);//undefined
    

此时我们查看一下obj的属性,可以发现obj.name=xxx其实是设置了一个【字符串类型】的属性
而var yourName = "name";则把这个字符串属性的值赋值给了变量yourName,所以你成了老王

console.log(Object.keys(obj));//输出["name"]

总结:

"."点只能取到【静态】的值
,[]则能够取到作为【变量动态】改变的值


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...