简体   繁体   English

jQuery:从具有一个类的多个实例的对象访问元素

[英]jQuery: Accessing an element from an object with multiple instances of a class

I am trying to do a simple tab navigation. 我正在尝试做一个简单的标签导航。

I was thinking I could put all buttons in an object, do the same with the content divs, get index of clicked button and access the content div using that index number to make the respective content show up. 我当时想我可以将所有按钮放在一个对象中,对内容div进行同样的操作,获取被单击按钮的索引,并使用该索引号访问内容div以显示相应的内容。 What am I doing wrong? 我究竟做错了什么?

HTML: HTML:

<div class="row">
    <a class="btn" href="#">One</a>
    <a class="btn" href="#">Two</a>
    <a class="btn" href="#">Three</a>
    <a class="btn" href="#">Four</a>
</div>
<div class="row">
    <div class="content">Content 1</div>
    <div class="content">Content 2</div>
    <div class="content">Content 3</div>
    <div class="content">Content 4</div>
</div>

jQuery: jQuery的:

$(document).ready(function(){
    var $btn = $('.btn');
    var $content = $('.content');

    $btn.each(function(){
        $(this).on('click', function(){
            $content.hide();
            var i = $btn.index(this);
            $content[i].show(); //This does not work
        });
    });
});

jsFiddle: http://jsfiddle.net/pcr0zuuo/ jsFiddle: http : //jsfiddle.net/pcr0zuuo/

Thanks in advance! 提前致谢!

Your issue is that show() is a jQuery function. 您的问题是show()是jQuery函数。 However doing the offset [i] off of a jQuery object will return you the raw dom element. 但是,对jQuery对象执行偏移量[i]会返回原始dom元素。 You should use eq(index) instead to get the jQuery object back to operate on. 您应该改用eq(index)使jQuery对象恢复运行。

Essentially the issue is that you tried using a jQuery function on a normal DOM node. 本质上,问题是您尝试在普通DOM节点上使用jQuery函数。 You can fix it in two ways: 您可以通过两种方式修复它:

$( $content[i] ).show()

or 要么

$content.eq(i).show()

Both should properly wrap the element and show it as you'd expect. 两者都应正确包装元素并按预期显示它。 ( http://jsfiddle.net/pcr0zuuo/1/ ) http://jsfiddle.net/pcr0zuuo/1/

jQuery's collections aren't arrays of jQuery objects, the array reference is to the DOM object. jQuery的集合不是jQuery对象的数组,数组引用是针对DOM对象的。 The collection of $content can't be referenced with [] to run jQuery functions. $ content的集合不能用[]引用来运行jQuery函数。 So change to: 因此更改为:

$content.eq(i).show();

and it works in your jsFiddle. 它可以在您的jsFiddle中使用。 (Thanks to Taplar for the hint) (感谢Taplar的提示)

You logic is correct. 你的逻辑是正确的。 You can target the index of the class .btn and show the corresponding divs. 您可以定位.btn类的索引并显示相应的div。

If you want to use jquery, use index() function to find index and eq to target the index. 如果要使用jquery,请使用index()函数来查找索引,并使用eq来定位该索引。

$('.btn').click(function()
{
    $('.content').eq($(this).index()).show();
});

http://jsfiddle.net/eqntjb01/ http://jsfiddle.net/eqntjb01/

Alternately, you can implement it via javascript ( the way you were trying to do in the example ) 或者,您可以通过javascript(在示例中尝试的方式)实现它

$('.btn').click(function()
{
  $('.content')[$(this).index()].style.display = "block";
});

http://jsfiddle.net/eqntjb01/1/ http://jsfiddle.net/eqntjb01/1/

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

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