【问题标题】:indicating various properties of html objects表示html对象的各种属性
【发布时间】:2017-08-14 08:45:54
【问题描述】:

我要设置html对象的属性。

var property1 = 'style.visibility';
var property2 = 'style.display';
var property3 = 'style';

我尝试了以下事情。

第一;

1;  object[property1] = 'visible';
2;  object[property2] = 'block';
3;  object[property3].display = 'none';

秒;

1;  object.property1 = 'visible';
2;  object.property2 = 'block';
3;  object.property3.display = 'none'; 

就我而言,只有 first;3; 运行良好。
有什么方法可以方便的表示html对象的属性?

【问题讨论】:

    标签: javascript html


    【解决方案1】:

    您可以使用reduce() 创建函数来访问嵌套属性。

    var property1 = 'style.visibility';
    var property2 = 'style.display';
    var property3 = 'style';
    
    var obj = {style: {visibility: 1, display: 2}}
    
    function getProp(prop, obj) {
      return prop.split('.').reduce(function(r, e) {
        return r[e]
      }, obj)
    }
    
    console.log(getProp(property1, obj))
    console.log(getProp(property2, obj))
    console.log(getProp(property3, obj))

    【讨论】:

    • 谢谢,reduce 将极大地支持我的编程。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-05
    • 2011-06-17
    相关资源
    最近更新 更多