简体   繁体   English

如何针对这个李类的兄弟姐妹?

[英]How to target the siblings of this li class?

http://codepen.io/leongaban/pen/dCHwI http://codepen.io/leongaban/pen/dCHwI

I have a basic nav, the li's default image is a white ball, the selected nav button has a blue ball. 我有一个基本的导航,li的默认图像是一个白色的球,所选的导航按钮有一个蓝色的球。

I'm able to get the id of the nav button that is clicked and switch it's img src. 我可以获得单击的导航按钮的ID并将其切换为img src。 However I have not been able to target it's siblings, the other li's img src :( 但是我无法针对它的兄弟姐妹,另一个李的img src :(

So currently when you click other buttons, the pervious blue buttons stay blue. 因此,当前当您单击其他按钮时,先前的蓝色按钮保持蓝色。

在此处输入图片说明

How would you approach this? 您将如何处理?

HTML HTML

<footer id="tour_footer">
    <nav>
        <ul>
            <li id="tour_prev">< prev</li>

            <li id="tour_btn_1" class="tour_nav_circle">
                <img src="http://leongaban.com/_codepen/tour_nav_blue.gif" alt="tour 1" />
            </li>
            <li id="tour_btn_2" class="tour_nav_circle">
                <img src="http://leongaban.com/_codepen/tour_nav_white.gif" alt="tour 2" />
            </li>
            <li id="tour_btn_3" class="tour_nav_circle">
                <img src="http://leongaban.com/_codepen/tour_nav_white.gif" alt="tour 3" />
            </li>
            <li id="tour_btn_4" class="tour_nav_circle">
                <img src="http://leongaban.com/_codepen/tour_nav_white.gif" alt="tour 4" />
            </li>

            <li id="tour_next">next ></li>
        </ul>
    </nav>
</footer>

jQuery jQuery的

var buttonStates = function(id) {
  console.log('inside buttonStates function: '+id);
  var $navBtn = $(id);
  $navBtn.children().attr("src","http://leongaban.com/_codepen/tour_nav_blue.gif");
  $navBtn.parents('li').siblings('li img').attr("src","http://leongaban.com/_codepen/tour_nav_white.gif");

  console.log($(id));
}

// Button to switch Frames
$('.tour_nav_circle').unbind('click').bind('click', function (event) {
  var id = this.id;
  console.log(id);
  buttonStates('#'+id);
});
$navBtn.siblings('li').children('img')

$navBtn already seems to be a li element, so it doesn't have any li parents. $navBtn似乎已经是li元素,因此它没有li父母。 And since it doesn't have img siblings, li img never matches. 而且由于它没有img兄弟姐妹,因此li img永远不会匹配。

And FYI, instead of passing the ID to the function, you just pass the element itself. 仅供参考,您不必传递ID到函数,而只需传递元素本身。 You don't have to search for the element again if you already have a reference to it. 如果已经有了对元素的引用,则无需再次搜索。

buttonStates(this);

// ...
var buttonStates = function(el) {
    var $navBtn = $(el);
    // ...
};

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

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