Class abbreviation
Riot provides a brief syntax to generate class names, for example:
[code]<p class={ foo: true, bar: 0, baz: new Date(), zorro: 'a value' }></p>
Only true values will be generated, false values will be ignored. The generated results are as follows:
[code]<p class="foo baz zorro"></p>
Of course, you can use this feature on other attributes, if you find a suitable place~
Give me a cheating example:
[code]<h1 xxx={a: true, b: 1}></h1>
Of course it is generated, but it is also a scam:
[code]<h1 xxx="a b"></h1>
Bracket escaping
After escaping, "{" and "}" can be output.
[code]\{ 这里的表达式,将不会执行 \}
will output:
[code]{ 这里的表达式,将不会执行 }
Custom brackets
We can freely define the brackets for reading values, just like:
[code]riot.settings.brackets = '${ }' riot.settings.brackets = '{{ }}'
The space in the middle, It is the place where the expression value is placed
Binds unescaped HTML
Riot's expression can only bind text values other than HTML. However, you can bind HTML content through custom tags. For example:[code] <raw> <span></span> // 把这个标签的HTML,设为参数的content值 this.root.innerHTML = opts.content; </raw>
[code] <todo> <p> 这里有一些HTML的内容—— <raw content="{ html }"></raw> </p> this.html = '我叫:<strong>da宗熊</strong>'; </todo>
You can see that the todo tag is embedded with the raw tag. This is a very interesting thing in Riot.
## Nested HTMLIn Riot, there is a very important tag called
It can put the HTML embedded in the tag in use inside the tag definition.
[code] <my-tag> <!-- 这里使用了yield标签 --> <p>Hello <yield/></p> this.text = 'world' </my-tag>
[code] <my-tag> <!-- 这部分内容,会替换掉<yield />标签 --> <b>{ text }</b> </my-tag>
[code]<my-tag> <p>Hello <b>world</b></p> </my-tag>
如果自定义标签内,元素带有 id 或 name ,这些元素,会自动绑定到标签内容的 context 上,我们可以很简单的通过javascript,获取到这些元素进行操作:
[code] <login> <form id="login" onsubmit={ submit }> <input name="username"> <input name="password"> <button name="submit"> </form> // 上面的元素,已经绑定到this对象中了 var form = this.login, username = this.username.value, password = this.password.value, button = this.submit </login>
当然咯,这些带有 name 或 id 的元素,也可以在标签的HTML内使用,例如:
[code]<div>{ username.value }</div>
事件处理
Riot标签的事件定义,可以通过简单的方式进行绑定:[code] <login> <form onsubmit={ submit }></form> // 表单的提交,会运行下面的submit方法 submit(e) { } </login>
[code]<form onsubmit={ condition ? mA : mB}></form>
当然,我们可以在事件处理函数中,return true;来让默认事件正常触发。
Event对象
在所有的事件处理函数中,第一个参数,就是event对象。每个event对象,含有下面几个属性:
e.currentTarget 指向事件触发的那个元素
e.target 事件起源的元素,呃,跟e.currentTarget一样
e.which 键盘事件中的按键值【key code】
e.item 当前each循环中,元素绑定的this对象
以上就是riot.js学习【五】杂烩1的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!