Riotjs-accessing-dom
提供:Dev Guides
RIOT.JS-DOMへのアクセス
refsオブジェクトを使用してHTML要素にアクセスできます。 最初のステップとして、ref属性をDOM要素に追加し、タグのスクリプトブロックでthis.refを使用してそれにアクセスします。
- Attach ref -DOM要素にref属性を追加します。
<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;
};
})
例
以下は完全な例です。
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>
カスタム6
<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>
これにより、次の結果が生成されます–