jQuery 1.6.4 中的活动背景颜色切换
Active background color switch in jQuery 1.6.4
此 jQuery 代码更改除版本 1.6.4 之外的所有 jQuery 版本中的背景颜色。为什么版本 1.6.4 无法正确运行此代码?
.HTML:
<table cellspacing="0" cellpadding="0" id="bin" width="100%">
<thead>
<tr> <a href="#"><th style="text-align:left; padding-top: 20px;" width="10%" id="row-1">Symbol <img src="/images/sort-arrow-up.png" title="Sort by Symbol" alt="Sort by Symbol" class="sort-right move-left bottom-image" id="image1"/></th></a>
<th style="text-align:left;" width="20%" id="row-2">Company
<br><span class="move_right">Name</span>
<img src="/images/sort-arrow-up.png" title="Sort by Company Name" alt="Sort by Company Name" class="sort-right move-left" id="image2" />
</th>
<th style="text-align:center;" width="12%" id="row-3"><span class="center-text">Buy</span>
<br>Date
<img title="Sort by Buy Date" src="/images/sort-arrow.png" alt="Sort by Buy Date" id="image3" />
</th>
<th style="text-align:center;" width="10%" id="row-4"><span class="center-text">Buy</span>
<br>Price
<img title="Sort by Buy Price" src="/images/sort-arrow.png" alt="Sort by Buy Price" id="image4" />
</th>
<th style="text-align:center;" width="9%" id="row-5"><span class="center-text">Closed</span>
<br>Price
<img title="Sort by Closed Price" src="/images/sort-arrow.png" alt="Sort by Closed Price" id="image5" />
</th>
<th style="text-align:center;" width="9%" id="row-6"><span class="center-text">Closed</span>
<br>Date
<img title="Sort by Closed Date" src="/images/sort-arrow.png" alt="Sort by Closed Date" id="image6" />
</th>
<th style="text-align:center;" width="10%" id="row-7"><span class="center-text">Total</span>
j查询:
$(function(){
$('#bin').on('click', 'th', function(){
$(this).parent().children().removeClass('active');
$(this).addClass('active');
});
});
.CSS:
tr th.active
{
background-color: #7DAFFF;!important
}
演示 - js小提琴
我猜 jQuery 1.6.4 无法识别 .on
这是库 1.6.4 及更高版本之间的区别
http://jsperf.com/on-vs-delegate-jquery/3
.delegate 应该在 1.6.4 中工作后来他们把它改成 .on
这里是 click(function()
$(function(){
$('#bin th').click(function(){
$(this).parent().children().removeClass('active');
$(this).addClass('active');
});
});
例
我像这样尝试过(小提琴),它工作正常:
.JS:
$(function(){
$('#bin th').click(function(){
$(this).addClass('active').siblings().removeClass('active');
});
});
.CSS:
tr th.active
{
background-color: #7DAFFF;
}
事实证明,是.on方法导致了问题。
on
在jquery 中不受支持1.6.4
您可以使用delegate('th','click')
相关文章:
- 使用JS如何动态更改显示的html文件中的文本背景颜色
- 如何使用jquery更改html中的背景颜色
- Javascript对象类在单击时打开窗口进行颜色选择,并在更改时替换对象背景颜色
- JS幻灯片与CSS背景颜色变化
- DT数据表背景颜色为黑色,但仍有悬停和选择颜色
- Javascript通过列表项的函数和css来更改背景颜色
- Javascript没有从数组中选择背景颜色
- 使用圆中的数组更改背景颜色项目
- 如何用jquery动画改变背景颜色,就像一个过渡
- 使用jquery交换并保存背景颜色
- j查询如何设置/取消设置下拉菜单的背景颜色
- 使用angularjs根据时间更改背景颜色
- 设置Dojo量表的背景颜色
- javascript样式的背景颜色在设置时闪烁.ASP.NET
- 使用javascript更改任意文本的背景颜色
- 在用Javascript更改背景颜色后:hover don'不要改变颜色
- 图像内部的透明背景颜色不受影响
- jQuery mobile-动态更改ui页面背景颜色
- 切换按钮的背景颜色
- JavaScript更改类的背景颜色