首页 > 解决方案 > Javascript - 方法名称的变量?

问题描述

如何间接引用 javascript 对象?

认为:

<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

只能使用evalorwindow吗?

标签: javascriptvariablesindirect-objects

解决方案



推荐阅读