如何间接引用javascript对象?
假设:
GetMD5Hash
如何访问<div id="foo" data-munchy="bar" data-crunchy="baz">FooBar</div>
<script>
document.getElementById("foo").onclick = function() {
tempVariable = 'munchy';
console.log(this.dataset.tempVariable);
}
</script>
?在这种情况下,this.dataset.{someVariable}
是否只能使用this.dataset.tempVariable
或eval
?
答案 0 :(得分:4)
使用方括号表示法:
this.dataset[tempVariable];
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors