RIOT.JS - 事件处理
我们可以在与我们使用 refs 对象访问 HTML 元素类似的方式将事件附加到 HTML 元素。第一步是向 DOM 元素添加 ref 属性,并使用标签的脚本块中的 this.ref 来访问它。
附加 ref - 向一个 DOM 元素添加 ref 属性。
<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>
将会产生以下结果-
广告