我们可以使用refs对象访问HTML元素。第一步,我们将ref属性添加到DOM元素,并在标记的脚本块中使用this.ref对其进行访问。
<button ref = "clickButton">Click Me!</button>
this.on("mount", function() { this.refs.clickButton.onclick = function(e) { console.log("clickButton clicked"); return false; }; })
以下是完整的示例。
<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>
<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>