指示html对象的各种属性 [英] indicating various properties of html objects

查看:51
本文介绍了指示html对象的各种属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要设置html对象的属性.

I am going to set property of html objects.

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对象的属性吗?

In my case only first;3; was working well.
Is there any way to easily indicate the property of html objects?

推荐答案

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

You can create function using reduce() to access nested properties.

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))

这篇关于指示html对象的各种属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆