代码之家  ›  专栏  ›  技术社区  ›  pbarney

Javascript-方法名称的变量?

  •  0
  • pbarney  · 技术社区  · 6 年前

    如何间接引用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

    是否只能使用 eval window ?

    1 回复  |  直到 6 年前
        1
  •  4
  •   Kevin Boucher    6 年前