简体   繁体   English

旧Jquery过滤器在最新版本1.7.2中不起作用

[英]Old Jquery filter not working in latest version1.7.2

I have working copy of tab navigation in my application, i tried to update the jquery to latest(1.7.2) , for this i have downloaded jquery-1.7.2.min.js from jquery website . 我在我的应用程序中有标签导航的工作副本,我试图将jquery更新到最新版本(1.7.2),为此我从jquery网站下载了jquery-1.7.2.min.js。 After updating this the following line not working as expected tabContainers.hide().filter(':member').show(); 更新后,以下行无法正常工作tabContainers.hide().filter(':member').show();

here is the full jquery method. 这是完整的jquery方法。

                $(function () {
                var tabContainers = $('div.tabs > div');
                tabContainers.hide().filter(':member').show();

                $(window).bind('hashchange', function () {
                    var hash = window.location.hash || '#member';        
                    tabContainers.hide();
                    tabContainers.filter(hash).show();
                    $('div.tabs ul.tabNavigation a').removeClass('selected');
                    $('a[hash=' + hash + ']').addClass('selected');
                   document.getElementById("submitform").action="newaction.action"+hash;
                });

                $(window).trigger( "hashchange" );
            });

here is the html part 这是html部分

                <ul class="tabNavigation">
                    <li><a href="#member">Tab1 </a></li>
                    <li><a href="#tab2">tab2</a></li>
                    <li><a href="#tab3">tab3</a></li>
               </ul>
               <div id="member">content goes here</div>
               <div id="tab2 . . .

Update: changing :member to #member or :first loading the content of first div, but still the first li is not got selected.(members tab) 更新:更改:成员到#member或:首先加载第一个div的内容,但仍然没有选择第一个li。(成员选项卡)

Check this fiddle .With 1.3.2 and older version only filter('#id') works but not filter(':id') .I am not sure how it worked for you. 检查这个小提琴。使用 1.3.2和旧版本只有filter('#id')可以工作,但不能filter(':id') 。我不确定它是如何工作的。

Update tabContainers.hide().filter(':member').show(); 更新tabContainers.hide().filter(':member').show(); to tabContainers.hide().filter('#member').show() ; to tabContainers.hide().filter('#member').show() ;

First: With a working hashchange handler, the line tabContainers.hide().filter(':member').show(); 第一:使用工作的hashchange处理程序,行tabContainers.hide().filter(':member').show(); will be nullified when $(window).trigger("hashchange"); $(window).trigger("hashchange");时,它将被取消$(window).trigger("hashchange"); fires. 火灾。 Any attempt to fix tabContainers.hide().filter(':member').show(); 任何尝试修复tabContainers.hide().filter(':member').show(); is therefore futile and the statement can be deleted. 因此是徒劳的,声明可以删除。

Second: There's a cross-browser issue with location.hash , namely that some browsers return a string with a leading '#' and some don't. 第二: location.hash存在跨浏览器问题,即某些浏览器返回带有前导'#'的字符串,有些则不返回。 This requires the returned string to be normalized. 这需要对返回的字符串进行规范化。

Taking these factors into account, you might like to try: 考虑到这些因素,您可能会尝试:

$(function() {
    var tabContainers = $('div.tabs > div'),
        submitform = $("#submitform");

    $(window).on('hashchange', function () {
        var hash = (location.hash=='' || location.hash=='#') ? '#member' : location.hash;
        var hashNormalized = (hash.match(/^#/)) ? hash : '#'+hash;
        tabContainers.hide().filter(hashNormalized).show();
        $('div.tabs ul.tabNavigation a').removeClass('selected').filter(function() {
            return this.hash == hashNormalized;
        }).addClass('selected');
        submitform.attr('action', 'newaction.action' + hashNormalized);
    }).trigger('hashchange');
});

Tested in Opera 11.64 and IE9. 在Opera 11.64和IE9中测试过。 Be sure to test in FF 一定要在FF中测试

See DEMO DEMO

The .filter(function(){...}) approach to filtering the anchors should immunise you against differences between jQuery versions. 过滤锚点的.filter(function(){...})方法可以使你免受jQuery版本之间的差异。

You have to use either this 你必须使用这个

 tabContainers.hide().filter('#member').show();

or 要么

tabContainers.hide().filter(':first').show();

To show the first div with id member . 显示带有id member的第一个div。

And to highlight the corresponding a 并突出相应的a

Change this 改变这个

$('a[hash=' + hash + ']').addClass('selected');

To

$('a[href=' + hash + ']').addClass('selected');

As hash is not the attribute name, it's href . 由于hash不是属性名称,因此它是href

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM