3、标签的属性和属性值 h1 p 等等这些标签,我们通常都会给他们写上id & class属性的,那么这在 jade 中应该怎么写呢?和 zen coding 的语法一样,我们只需要这样写: h1#id.class this is a title. p#j-text.text this is a paragraph. 编译结果为:
this is a title.
this is a paragraph.
等等,那我要是想添加多个 class 怎么办呢?这样办: h1#id.class1.class2.class3 this is a title. p#j-text.text this is a paragraph. 编译结果为:
this is a title.
this is a paragraph.
什么?写 div 写烦了?那就不写咯。 #id.class #id.class1.class2 this is a div without tags. 编译结果为:
this is a div without tags.
这里要说明一下,在 jade 的语法里面,只有 div 标签能够省略不写. 说完了 id 和 class,我们再来说一下标签其他的属性应该怎么添加。jade 里添加其他属性和值的语法也和 zen coding 类似,我们需要在标签后面加上小括号(),然后按照(属性名=属性值)的格式写就好了,如果有多个属性,中间以逗号进行分割。 比如上面的 id 和 class 的写法我们就可以改写成: h1(id="id", class="class") this is a title. p(id="j-text", class="text") this is a paragraph. 结果是一样的:
this is a title.
this is a paragraph.
说来说去还是这两个属性,烦了?那我们换一个吧: a(herf="/index.html", title="this is a link.", target="_blank", data-uid="1000") index.html 编译结果为: index.html 那么问题就来了,如果我们要写一个单属性应该怎么写?比如给表单元素添加 checked属性: input(type="checkbox", name="all", checked, value="全选") 编译结果为: