jQuery 两部分问题 - 锚点模拟选项卡和绑定单击事件

发布于 2024-09-30 19:44:44 字数 2563 浏览 10 评论 0原文

问题


  1. 我想在页面中有一个锚点来执行与选项卡相同的功能。
    只有在特定锚点的 onclick 事件中放置类似 $('#tabs ul a:first').click() 的内容,我才能使其正常工作。注意:href 对选项卡插件外部的链接没有影响;它可能只在构建点击事件时用在选项卡中的锚点中。

  2. 我遇到了 jQuery 绑定问题。
    与其对每个链接执行 DOM 查找,不如将其存储在变量中并将单击事件添加到外部锚点的单击事件中。但是,当选择器返回多个对象时,数组中的对象(很可能不是返回的数组,而是一个对象)似乎没有任何功能。

JSFiddle 示例


JS


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

HTML/CSS(不完整)


<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>

Question


  1. I'd like to have an anchor in the page that performs the same function as the tab.
    I was only able to get it to work if I placed something like $('#tabs ul a:first').click() in the onclick event of the particular anchor. Note: the href has no effect on the links external to the tab plugin; it is probably only used in the anchors in the tab when constructing their click event.

  2. I came across a jQuery binding problem.
    Instead of performing a DOM lookup for each link, it'd be better to store that in a variable and add the click event to the external anchor's click event. However, when a selector returns multiple objects, the object in the array (it's most likely not an array that's returned, but an object) does not seem to have any functions.

JSFiddle Example

JS


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

HTML/CSS (incomplete)


<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云胡 2024-10-07 19:44:44

相反,

(tabs[index]).click();

它应该是

tabs.eq(index).click();

第一个表单为您提供原始 DOM 元素,而第二个表单为您提供 jQuery 包装的 DOM 元素。 元素节点上没有“click”方法,至少没有标准方法。 (或者,$(tabs[index]) 也可以。)

Instead of

(tabs[index]).click();

it should be

tabs.eq(index).click();

The first form gets you the raw DOM element, while the second gets you a jQuery-wrapped DOM element. There's no "click" method on <a> element nodes, at least no standard method. (Alternatively, $(tabs[index]) would work too.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文