3

我在玩 Riot.js,一切都很好。但是假设我有一个页面,我在其中安装了这个单个标签:

<so-example>

  <input type="text />

  <script>
    this.disabled = false
  </script>

</so-example>

假设我想查询这个元素的其中一个属性(例如,如果它被禁用)。这两种方法都不起作用:

  1. document.getElementsByTagName('so-example')[0].disabled
  2. document.querySelector('so-example').disabled

这两个语句都返回undefined。我希望我的标签的 DOM API 反映其状态,但无法弄清楚我在这里缺少什么。

4

3 回答 3

3

对于发现自己处于相同情况的任何人,答案是查询_tag元素上的属性。要访问disabled我在原始问题中使用的元素的属性,您可以这样做:

document.querySelector('so-example')._tag.disabled

这应该返回预期的false. this可以通过这种方式访问​​组件内定义的任何内容。

于 2016-10-26T18:44:46.203 回答
0

我可能错了,因为我从未使用过 riot.js,它可以为你做某种神奇的编译,但我对此表示怀疑,这听起来有点矫枉过正......

自定义元素上的属性不会绑定到它们的 JS 表示。您不能将.disable其用作快捷方式,因为 querySelector 或 getElementByWhatever 函数返回一个HTMLUnknownElement没有任何绑定属性的。所以你必须使用getAttribute('disabled');orhasAttribute('disabled');代替。

于 2016-10-13T22:35:30.640 回答
0

这取决于您要从哪里访问该物业。例如,如果它来自父标签,那么您可以使用。this.tags.child.message(从孩子访问消息属性)

<example>
  <child></child>
  <button onclick={access_child}>access child</button>
  <script>
      access_child() {
        console.log(this.tags.child.message) 
      }
  </script>
</example>

<child>
  <script>
      this.message = 'Hello Riot'
  </script>
</child>

如果你想从 index.html 访问,你应该riot.compile像这样包装挂载

<script type="text/javascript">
    riot.compile(function() {
      var example_tag = riot.mount('example')[0]
      var child_tag = example_tag.tags.child
      console.log('from index: '+child_tag.message)
    })
</script>

这是一个工作示例http://plnkr.co/edit/mjLlDozVzzfcv3Nwl63Y?p=preview

于 2016-10-14T17:45:58.107 回答