RIOT.JS - 事件处理
我们可以将事件附加到 HTML 元素,就像我们使用 refs 对象访问 HTML 元素一样。第一步,我们向 DOM 元素添加 ref 属性,并在标签的脚本块中使用 this.ref 访问它。
Attach ref - 将 ref 属性添加到 DOM 元素。
<button ref = "clickButton">Click Me!</button>
使用 refs 对象- 现在在挂载事件中使用 refs 对象。当 RIOT 安装自定义标签并填充 refs 对象时,会触发此事件。
this.on("mount", function() {
console.log("Mounting");
console.log(this.refs.username.value);
})
例子
以下是完整的示例。
自定义5Tag.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>
定制5.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>
这将产生以下结果 -