我们可以使用类似于使用refs对象访问HTML元素的方式,将事件附加到HTML元素。第一步,我们将ref属性添加到DOM元素,并在标记的脚本块中使用this.ref对其进行访问。
- 附加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);
})例子
以下是完整的示例。
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>作者:terry,如若转载,请注明出处:https://www.web176.com/riotjs/2193.html
支付宝
微信