admin管理员组文章数量:1402848
Hello I got a question regarding setting the display of a div with the use of the data-target
attribute. I have the following HTML code:
<div class="page page-current" id="home">
PAGE 1
<a href="#" class="next" data-target="#about">Go to about</a>
</div>
<div class="page page-section" id="about">
PAGE 2
<a href="#" class="next" data-target="#portfolio">Go to portfolio</a>
</div>
<div class="page page-section" id="portfolio">
PAGE 3
</div>
As can seen above, the div's that contain the class: page-section
are initial hidden by CSS using: display:none
. Now I managed to create a generic JS code that checks which div should be made visible:
$(".next").click(function(){
var target = $(this).data("target");
$(target).addClass("page-current");
})
The problem with this is that it adds to the second div the class: page-current
but the first div also still contains this class.
Can anyone guide me in the right direction how I could solve this is in a generic way so that only one div at the time can contain the class: page-current
I have created a JSFIDDLE of this problem.
Hello I got a question regarding setting the display of a div with the use of the data-target
attribute. I have the following HTML code:
<div class="page page-current" id="home">
PAGE 1
<a href="#" class="next" data-target="#about">Go to about</a>
</div>
<div class="page page-section" id="about">
PAGE 2
<a href="#" class="next" data-target="#portfolio">Go to portfolio</a>
</div>
<div class="page page-section" id="portfolio">
PAGE 3
</div>
As can seen above, the div's that contain the class: page-section
are initial hidden by CSS using: display:none
. Now I managed to create a generic JS code that checks which div should be made visible:
$(".next").click(function(){
var target = $(this).data("target");
$(target).addClass("page-current");
})
The problem with this is that it adds to the second div the class: page-current
but the first div also still contains this class.
Can anyone guide me in the right direction how I could solve this is in a generic way so that only one div at the time can contain the class: page-current
I have created a JSFIDDLE of this problem.
Share Improve this question asked Jul 27, 2016 at 14:28 Rotan075Rotan075 2,6155 gold badges37 silver badges56 bronze badges1 Answer
Reset to default 6To do what you require you need to remove the page-current
from the current page, and add it to the new element, while removing page-section
from the new element and adding it back on to the old.
You can do this by using the toggleClass()
method, like this:
$(".next").click(function() {
var target = $(this).data("target");
$('.page-current').add(target).toggleClass('page-current page-section');
})
Working example
本文标签: javascriptSet display of div by using the datatarget attributeStack Overflow
版权声明:本文标题:javascript - Set display of div by using the data-target attribute - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744355175a2602269.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论