代码之家  ›  专栏  ›  技术社区  ›  vol7ron

jQuery两部分问题-锚定到模拟选项卡并绑定click事件

  •  3
  • vol7ron  · 技术社区  · 14 年前


    1. 我希望页面中有一个锚,它执行与选项卡相同的功能。
      只有当我把一些东西放在 $('#tabs ul a:first').click() 在特定锚的onclick事件中。注意:href对tab插件外部的链接没有影响;它可能只在构建其click事件时用于tab中的锚。

    2. 我遇到了一个jQuery绑定问题。

    JSFiddle Example


    JS公司


    $(function() {
        $( "#tabs" ).tabs(); 
    
        var tabs = $('#tabs ul li a');
    
        $( "#links2 a" ).each(function(index){
           $(this).bind('click',function(){
              (tabs[index]).click();
           });
        });
    });
    


    <link rel="stylesheet" href="http://jqueryui.com/themes/base/jquery.ui.all.css" />
    <link rel="stylesheet" href="http://jqueryui.com/demos/demos.css" />
    <style type="text/css">
       #links1 , #links2, #links3      { margin-top:1em; font-size:1.5em;}
       #links1 a, #links2 a, #links3 a { text-decoration:none; color:#0cf}
    </style>
    <div id="tabs">
        <ul>
            <li><a href="#tabs-1">Foo</a></li>
            <li><a href="#tabs-2">Bar</a></li>
            <li><a href="#tabs-3">Baz</a></li>
        </ul>
        <div id="tabs-1" ><p>Foo foo foo foo foo foo foo foo. foo-foo!</p></div>
        <div id="tabs-2" ><p>Bar bar bar bar bar bar bar bar. bar-bar!</p></div>
        <div id="tabs-3" ><p>Baz baz baz baz baz baz baz baz. baz-baz!</p></div>
    </div>
    
    <div id="links1">
       <div>HREF doesn't matter:</div>
       <a href="#tabs-1">foo</a>     
       <a href="#tabs-2">bar</a>           
       <a href="#tabs-3">baz</a>
    </div>
    
    <div id="links2">
       <div>Trying to dynamically bind onclick</div>
       <a href="#">foo</a> 
       <a href="#">bar</a>       
       <a href="#">baz</a>
    </div>
    
    <div id="links3">
       <div>What should happen, but not the right method:</div>
       <a href="#" onclick="$('#tabs li:first a').click()">foo</a>
       <a href="#" onclick="$('#tabs li a:eq(1)').click()//not sure if this is the best way to reference">bar</a>       
       <a href="#" onclick="$('#tabs li:last a').click()">baz</a>
    </div>
    
    1 回复  |  直到 14 年前
        1
  •  2
  •   Pointy    14 年前

    而不是

    (tabs[index]).click();
    

    tabs.eq(index).click();
    

    第一个表单获取原始的DOM元素,第二个表单获取jQuery包装的DOM元素。没有“点击”方法 <a> $(tabs[index]) 也会起作用。)