Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
151 views
in Technique[技术] by (71.8m points)

javascript - Jquery custom attributes

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...