jQuery 1.6.4中的活动背景颜色切换

时间:2013-11-04 15:47:37

标签: javascript jquery html css

此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 &nbsp;
                <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 &nbsp;
                <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 &nbsp;
                <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>

jQuery的:

$(function(){
    $('#bin').on('click', 'th', function(){
        $(this).parent().children().removeClass('active');
        $(this).addClass('active');
    });
});

CSS:

tr th.active
{
  background-color: #7DAFFF;!important
}

DEMO - jsFiddle

3 个答案:

答案 0 :(得分:1)

我猜jQuery 1.6.4无法识别.on

这是库1.6.4和更高版本之间的差异

http://jsperf.com/on-vs-delegate-jquery/3

.delegate应该在1.6.4中工作 后来他们换了.on

这是点击(function()

    $(function(){
       $('#bin th').click(function(){
          $(this).parent().children().removeClass('active');
          $(this).addClass('active');
       });
    });

EXAMPLE

答案 1 :(得分:0)

我尝试了这样(fiddle)并且工作正常:

JS:

$(function(){
    $('#bin th').click(function(){
        $(this).addClass('active').siblings().removeClass('active');
    });
});

CSS:

tr th.active
{
  background-color: #7DAFFF;
}

原来是导致问题的.on方法。

答案 2 :(得分:0)

jquery on不支持

1.6.4您可以使用delegate('th','click')