RIOT.JS教程:访问DOM

我们可以使用refs对象访问HTML元素。第一步,我们将ref属性添加到DOM元素,并在标记的脚本块中使用this.ref对其进行访问。

  • 附加ref-将ref属性添加到DOM元素。
<button ref = "clickButton">Click Me!</button>
  • 使用refs对象-现在在挂载事件中使用refs对象。当RIOT装入自定义标记并填充refs对象时,将引发此事件。
this.on("mount", function() {
   this.refs.clickButton.onclick = function(e) {
      console.log("clickButton clicked");
      return false;
   };
})

例子

以下是完整的示例。

custom6Tag.tag

<custom6Tag>
   <form ref = "customForm">
      <input ref = "username" type = "text" value = "Mahesh"/>
      <button ref = "clickButton">Click Me!</button>
      <input type = "submit" value = "Submit" />
   </form>
   <script>
      this.on("mount", function() {
         this.refs.clickButton.onclick = function(e) {
            console.log("clickButton clicked");
            return false;
         };
         this.refs.customForm.onsubmit = function(e) {
            console.log("Form submitted");
            return false;
         };
      }) 
   </script>
</custom6Tag>

custom6.htm

<html>
   <head>
      <script src = "https://cdnjs.cloudflare.com/ajax/libs/riot/3.13.2/riot+compiler.min.js"></script>
   </head>
   <body>
      <custom6Tag></custom6Tag>
      <script src = "custom6Tag.tag" type = "riot/tag"></script>
      <script>
         riot.mount("custom6Tag");
      </script>
   </body>
</html>

作者:terry,如若转载,请注明出处:https://www.web176.com/riotjs/2191.html

(0)
打赏 支付宝 支付宝 微信 微信
terryterry
上一篇 2021年3月31日 下午3:42
下一篇 2021年3月31日 下午3:46

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注