Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想知道是否可以从元素数据中获取对象属性。
所以假设我有元素:
<div data-item="objProp" class="test">test</div>
和 js 对象:
var obj = { objProp: true }
我尝试过了:
var test = $('.test').data('item'); alert(obj.test);
所以,是的,有什么办法可以做到这一点吗?
你似乎想要
alert(obj[test]);
Working FIDDLE Demo
尝试使用[]:
[]