来由

在一些插件中经常看到, 在class属性中出现一些跟样式无关的属性值, 这些值在css样式中没有对应定义, 但是在js中会根据这个值来给dom对象添加特殊的行为, 例如:

jquery validate:

from http://www.cnblogs.com/hejunrex/archive/2011/11/17/2252193.html

<p>
<label for="email">E-Mail</label>
<input id="email" name="email" class="required email" />
</p>

jquery easy ui

<input id="pwd" name="pwd" type="password" class="easyui-validatebox" data-options="required:true">

为什么要在class中添加非样式内容?

原因

1、 借用class的含义, 类的含义, 标明此dom对象 是属于某一类事物, 事实上html标准上确实有此说法。

http://www.w3.org/TR/1999/REC-html401-19991224/struct/global.html#h-7.5.2

The class attribute, on the other hand, assigns one or more class names to an element; the element may be said to belong to these classes. A class name may be shared by several element instances. The class attribute has several roles in HTML:

  • As a style sheet selector (when an author wishes to assign style information to a set of elements).
  • For general purpose processing by user agents.

2、 class 索引速度比较快。

关于class索引速度实验

关于使用class索引, 与其对应的可以使用attribute索引, 可以进行这两类方法的对比。

使用 firefox 测试, jquery语法, selector 包括下面四种形式:

.selector

[selector=’selector’]

[class*=’selector’]

[selecotr*=’selector’]

code: 生成500个li对象,填充到#test中, 对于四种方式分别执行1000此索引。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title>TestHTML</title>

<script src="jquery.js" type="text/javascript"></script>

<meta http-equiv="content-type" content="text/html; charset=utf-8" />

</head>

<body>

<div id="TimeScalar" style="width:0;height:0;border-left: 5px solid transparent;border-right: 5px solid transparent;border-bottom: 10px solid #cc0606;">

</div>

<ul>

<li>

<ul id="test">

<li><a href="">first</a></li>

<li><a href="">second</a></li>

<li><a href="">third</a></li>

<li><a href="">forth</a></li>

</ul>

lllll

</li>

<li>ooooo</li>

</ul>

<script>

var i = 500;

var ul = $("#test");

ul.html('');

while (i > 0) {

ul.append("<li component=\"list-item my-other-class\" component1=\"list-item\" class=\"list-item my-other-class\">List Item " + i.toString() + "</li>");

i -= 1;

}

/* */

var starttime = (new Date()).getTime();

for (var i = 0; i < 1000; i++) {

$("[component*='list-item']");

};

var endtime = (new Date()).getTime();

console.log("attr selector consume time ="+(endtime - starttime))

var starttime = (new Date()).getTime();

for (var i = 0; i < 1000; i++) {

$("[component1='list-item']");

};

var endtime = (new Date()).getTime();

console.log("attr1 selector consume time ="+(endtime - starttime))

var starttime = (new Date()).getTime();

for (var i = 0; i < 1000; i++) {

$("[class*='list-item']");

};

var endtime = (new Date()).getTime();

console.log("class selector consume time ="+(endtime - starttime))

var starttime = (new Date()).getTime();

for (var i = 0; i < 1000; i++) {

$(".list-item");

};

var endtime = (new Date()).getTime();

console.log("class formal selector consume time ="+(endtime - starttime))

</script>

</body>

</html>

结果:

发现, .selector 方式最快, 对应结果最后一个 46ms

其次是, [selector=’selector’],  对应结果是 92ms

然后是, [selector*=’selector’], 对应结果是 135ms

最后是, [class*=’selector’], 对应结果是 153ms

attr selector consume time =135

test.html (第 54 行)

attr1 selector consume time =92

test.html (第 61 行)

class selector consume time =153

test.html (第 68 行)

class formal selector consume time =46

所以我们看到 .selector 是很有优势的, 所以各种插件有在class中写非样式标签的写法。

同时,我们也要修正对class的认识, 其并不是仅仅是只能容纳 stylesheet中,定义好的样式标签名。

补充实验: [component1] 耗时76秒。 也没有.selector快。

class属性中为什会添加非样式的属性值?的更多相关文章

  1. JQuery中对option的添加、删除、取值

    jQuery获取Select选择的Text和Value: 1. $("#select_id").change(function(){//code...});    //为Selec ...

  2. JS 对html标签的属性的干预以及JS 对CSS 样式表属性的干预

      -任何标签的任何属性都可以修改! -HTML里是怎么写, JS就怎么写   以下是一段js 作用于 css 的 href的 代码   <link id="l1" rel= ...

  3. Oracle复合B*tree索引branch block内是否包含非先导列键值?

    好久不碰数据库底层细节的东西,前几天,一个小家伙跑来找我,非要说复合b*tree index branch block中只包含先导列键值信息,并不包含非先导列键值信息,而且还dump了branch b ...

  4. ARC工程中添加非ARC文件

    转载自:http://blog.csdn.net/zhenweicao/article/details/16988543 分类: IOS2013-11-27 17:02 626人阅读 评论(0) 收藏 ...

  5. 为SQL Server表中的列添加/修改/删除注释属性(sp_addextendedproperty、sp_updateextendedproperty、sp_dropextendedproperty)

    本篇基本完全参考:sql--sp_addextendedproperty和sp_updateextendedproperty (Transact-SQL) 三个存储过程用法一样,以sp_addexte ...

  6. 将src非空的属性注入到des中

    package lizikj.bigwheel.common.vo.merchandise.util; import java.lang.reflect.Field; import lizikj.bi ...

  7. js中 给json对象添加属性和json数组添加元素

    js中 给json对象添加新的属性 比如现在有一个json对象为jsonObj,需要给这个对象添加新的属性newParam,同时给newParam赋值为pre.做法如下: var obj={ &quo ...

  8. eclipse 中main()函数中的String[] args如何使用?通过String[] args验证账号密码的登录类?静态的主方法怎样才能调用非static的方法——通过生成对象?在类中制作一个方法——能够修改对象的属性值?

    eclipse 中main()函数中的String[] args如何使用? 右击你的项目,选择run as中选择 run configuration,选择arguments总的program argu ...

  9. ClientDataSet中修改,删除,添加数据和Delta属性

    ClientDataSet中使用Post提交变更的数据时,实际上并没有更新到后端数据库中,而是提交到了由DataSnap管理的数据缓冲区中.当使用了ClientDataSet.ApplyUpDates ...

随机推荐

  1. MySQL并发调优和IO调优

    一.myisam的IO调优1.myisam通常在每次写入后把索引的改变刷写到磁盘上.所以批处理通常会更快点.做到这点,可以通过LOCK TABLES,他可以把写入控制到对表解锁.还可以用delay_k ...

  2. MySQL线程独享[转]

    一.前言在 MySQL 中,线程独享内存主要用于各客户端连接线程存储各种操作的独享数据,如线程栈信息,分组排序操作,数据读写缓冲,结果集暂存等等,而且大多数可以通过相关参数来控制内存的使用量。 二.线 ...

  3. but this usually doesn’t gain you anything.

    High Performance My SQL, Third Edition Date and Time Types My SQL has many types for various kinds o ...

  4. 在Delphi中如何动态创建dbf数据库(二)?

    unit Form_ToChangCSVforDBFU; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics ...

  5. Jquery元素选取、常用方法;js只能获取内联样式,jquery内联内嵌都可以获取到;字符串.trim();去字符串前后空格

    一:常用的选择器: 基本选择器 $("#myDiv") //匹配唯一的具有此id值的元素 $("div") //匹配指定名称的所有元素 $(".myC ...

  6. This system is not registered with RHN

    在红帽EL5上运行yum,提示“This system is not registered with RHN”,意思是没有在官网上注册,不能下载RH的软件包,替代方案是采用centos源. 1.卸载r ...

  7. 轮询、select、 epoll

    网卡设备对应一个中断号, 当网卡收到网络端的消息的时候会向CPU发起中断请求, 然后CPU处理该请求. 通过驱动程序 进而操作系统得到通知, 系统然后通知epoll, epoll通知用户代码.  一. ...

  8. js创建元素

    js创建多条数据,插入到页面中的方法. 方法一: 执行时间大概在35ms左右. 这个就属于使用字符串拼接之后,再一次性的插入到页面中.缺点是,容易导致事件难以绑定. 方法二: 执行时间不定,最少的在8 ...

  9. Array原型链添加“遍历”方法

    <script> //1.在我们之前的项目里向原型链中集成方法时大多代码分析不严密,有时间我在这里会做详细分析; Array.prototype.each = function(fn) { ...

  10. Wordpress更换编辑器

    这里我更换为KindEditor 1.下载插件 https://wordpress.org/plugins/kindeditor-for-wordpress/ 2.启动插件 3.在 设置 – Kind ...