RIOT.JS - 访问 DOM
我们可以使用 refs 对象访问 HTML 元素。第一步,我们向 DOM 元素添加 ref 属性,并在标签的脚本块中使用 this.ref 访问它。
Attach 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;
};
})
例子
以下是完整的示例。
自定义6Tag.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>
定制6.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>
这将产生以下结果 -