RIOT.JS - 访问 DOM
我们可以使用 ref 对象访问 HTML 元素。作为第一步,我们向 DOM 元素添加一个 ref 属性,并通过标签的脚本块中的 this.ref 访问它。
附加引用 − 向 DOM 元素添加 ref 属性。
<button ref = "clickButton">Click Me!</button>
使用 refs 对象 − 现在在 mount 事件中使用 refs 对象。此事件在 RIOT 挂载自定义标签时触发,并且它填充 refs 对象。
this.on("mount", function() {
this.refs.clickButton.onclick = function(e) {
console.log("clickButton clicked");
return false;
};
})
示例
以下是完整示例。
custom6Tag.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>
custom6.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>
这将产生以下结果 −
广告