开发者

set class for multiple div's

I am using a mouseover effect for multiple div's. The mouseover manipulates the class of all the div's, setting one as "class='active'" so it can be seen. Now I try to "reset" the changes on a certain event, for instance on 'click'. My mouseoverscript (using jQuery) is as follows

$(document).ready(function() {
    switches = $('#switches > span');
    slides = $('#slides > div');
    switches.each(function(idx) {
            $(this).data('slide', slides.eq(idx));
        }).hover(
        function() {
            switches.removeClass('active');
            slides.removeClass('active');             
            $(this).addClass('active');  
            $(this).data('slide').addClass('a开发者_C百科ctive');
        });
    });

And now I have multiple mouseover div's resulting in following html file

<html>
<head>

<title>test</title>

<script type="text/javascript" src="jQuery.js"></script>
<script type="text/javascript" src="switch.js"></script>

<style type="text/css">

#switches .active {
  font-weight: bold;
}

#slides div {
  display: none;
}

#slides div.active {
  display: block;
}

</style>

</head>
<body>


<div id="slides">
  <div id="slide1" class="active">Well well.</div>
  <div id="slide2">Oh no!</div>
  <div id="slide3">You again?</div>
  <div id="slide4">I'm gone!</div>
</div>

<div id="switches">
  <span id="switch1" class="active">First slide</span>
  <span id="switch2">Second slide</span>
  <span id="switch3">Third slide</span>
  <span id="switch4">Fourth slide</span>
</div>


<br><a href = "javascript:void(0)" onclick ="switches.removeClass('active');slides.removeClass('active');$(switch1).attr('class','active');$(slide1).attr('class','active')">Reset</a>

<div id="slides">
  <div id="slide1" class="active">Well well.</div>
  <div id="slide2">Oh no!</div>
  <div id="slide3">You again?</div>
  <div id="slide4">I'm gone!</div>
</div>

<div id="switches">
  <span id="switch1" class="active">First slide</span>
  <span id="switch2">Second slide</span>
  <span id="switch3">Third slide</span>
  <span id="switch4">Fourth slide</span>
</div>

For the first div group the reset works well, but somehwo the second stays unaffected. How can I work around this problem?


IDs must be unique. You should change switches and slides to classes instead of IDs.


I think the issue has to do with the divs having the same id. Try naming the 2nd group like slides_2 and switches_2 and do a select on the partial ID $(id^="slides").

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜