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>
將會產生以下結果-
廣告