Jquery自定义属性 [英] Jquery custom attributes

查看:139
本文介绍了Jquery自定义属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑在Jquery中使用自定义属性,以避免使用类或id属性,不会干扰html设计师。



考虑到这个想法, html应该是这样的:

 < ul Jquery =CommonUl> 
< li Jquery =CommonLi>< / li>
< li Jquery =CommonLi>< / li>
< li Jquery =CommonLi>< / li>
< li Jquery =CommonLi>< / li>
< li Jquery =CommonLi>< / li>
< / ul>




  • 你认为这是一个好方法吗? p>


  • 您对这些自订属性的W3C验证有什么看法?



  • 关于性能,我想用类属性或ids jquery选择器是比使用自定义属性和过滤功能像包含,等...。这是正确的吗?



解决方案

=http://ejohn.org/blog/html-5-data-attributes/> data - 属性( HTML5规范的一部分),如下所示:

 < li data-something =CommonLi>< / li> 

jQuery甚至在1.4.3+中内置了对这些的支持,例如:

  $(li)。data(something)//CommonLi

对于您的其他问题:




  • >

  • 这不应该干扰设计师,但它将依赖于 设计者

  • 如果您从某个元素获取,性能与任何其他属性相同


I'm thinking about using custom attributes in Jquery to avoid using class or id attributes, to not interfere with the html designers.

Taking into account this idea, the html should be pieces like:

<ul Jquery="CommonUl">
    <li Jquery="CommonLi"></li>     
    <li Jquery="CommonLi"></li>
    <li Jquery="CommonLi"></li>
    <li Jquery="CommonLi"></li>
    <li Jquery="CommonLi"></li>
</ul>

  • Do you think this is a good approach?

  • What is your opinion about the W3C validation of these custom attributes?

  • Do you know any way to program with Jquery without interfering with the work of the html designers?

  • About the performance, I suppose that with class attributes or ids the jquery selectors are faster than using a custom attribute and filtering functions like "contains, etc...". Is this right?

解决方案

You can do this, use data- attributes though (part of the HTML5 specification), like this:

<li data-something="CommonLi"></li> 

jQuery even has built-in support for these in 1.4.3+, for example:

$("li").data("something") //"CommonLi"

For your other questions:

  • They'll validate if it's HTML5 - but won't break anything in HTML4
  • This shouldn't interfere with the designer, but it'll depend on which designer
  • If you're fetching from an element, the performance is the same as any other attribute

这篇关于Jquery自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆