RIOT.JS - 事件处理

我们可以将事件附加到 HTML 元素,其方式与使用 refs 对象访问 HTML 元素的方式类似。首先,我们向 DOM 元素添加一个 ref 属性,然后使用标签脚本块中的 this.ref 访问它。

  • 附加 ref − 将 ref 属性添加到 DOM 元素。

<button ref = "clickButton">Click Me!</button>
  • 使用 refs 对象 − 现在在 mount 事件中使用 refs 对象。当 RIOT 安装自定义标签并填充 refs 对象时,将触发此事件。

this.on("mount", function() {
   console.log("Mounting");
   console.log(this.refs.username.value);
})

示例

以下是完整示例。

custom5Tag.tag

<custom5Tag>
   <form>
      <input ref = "username" type = "text" value = "Mahesh"/>
      <input type = "submit" value = "Click Me!" />
   </form>
   <script>
      this.on("mount", function() {
         console.log("Mounting");
         console.log(this.refs.username.value); 
      })
   </script>
</custom5Tag>

custom5.htm

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

这将产生以下结果 −