[英]Toggle div text on click
这是我的 jQuery
jQuery(document).ready(function($) {
$("#accordion").find(".accordion-toggle").click(function(){
$(this).next().slideToggle("fast");
$(".accordion-content").not($(this).next()).slideUp("fast");
});
});
这是我的 HTML
<div id="accordion">
<header class="accordion-toggle">
<h2>Accordion Title <span id="accordionIcon">▼</span></h2>
</header>
<section class="entry accordion-content">
<p>Accordion Content</p>
</section>
</div>
每当单击新的accordion-toggle
,我需要将旧的accordionIcon
更改为相反的箭头,新的也需要更改。 我试过使用$(".accordion-content").not($(this).next()).parent().find('#accordionIcon')
但它找不到正确的元素
这是一个小提琴。 这是你想要的?
这是我添加的代码。
if($(this).find("span#accordionIcon").text()=="▼"){
$(this).find("span#accordionIcon").text("▲");
}
else{
$(this).find("span#accordionIcon").text("▼");
}
接受的答案仅适用于一个切换。 这是版本( Codepen ),适用于多个:
HTML
<div id="accordion">
<header class="accordion-toggle">
<h2>Accordion Title 1<span>▲</span></h2>
</header>
<section class="entry accordion-content">
<p>Accordion Content</p>
</section>
<header class="accordion-toggle">
<h2>Accordion Title 2<span>▲</span></h2>
</header>
<section class="entry accordion-content">
<p>Accordion Content</p>
</section>
</div>
JS
jQuery(document).ready(function($) {
$("#accordion").find(".accordion-toggle").click(function(){
if ($(this).find('span').text() == '▼') {
$(this).siblings(".accordion-content").slideUp("fast");
$(this).siblings(".accordion-toggle").find('span').text('▼');
$(this).next().slideDown("fast");
$(this).find('span').text('▲');
} else {
$(this).next().slideUp("fast");
$(this).find('span').text('▼');
}
});
});
或者不改变你的代码,你可以这样做:
jQuery(document).ready(function($) {
$("#accordion").find(".accordion-toggle").click(function(){
var span = $(this).find('span');
if (span.hasClass('isOpened')) {
span.removeClass('isOpened').html('▲');
} else {
span.addClass('isOpened').html('▼');
}
$(this).next().slideToggle("fast");
$(".accordion-content").not($(this).next()).slideUp("fast");
});
});
如果你想使用 font-awesome
jQuery(document).ready(function($) { $("#accordion").find(".accordion-toggle").click(function(){ $(this).next().slideToggle("fast"); if($(".fa").hasClass("fa-arrow-down")){$(".fa").removeClass("fa-arrow-down").addClass("fa-arrow-up");} else $(".fa").removeClass("fa-arrow-up").addClass("fa-arrow-down"); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css"> <div id="accordion"> <header class="accordion-toggle"> <h2>Accordion Title <i class="fa fa-arrow-down"></i></h2> </header> <section class="entry accordion-content"> <p>Accordion Content</p> </section> </div>
jQuery(document).ready(function($) {
$("#accordion").find("a").click(function(){
//'all' triangles returned to initial position, because some of triangle is
//up but wich one? User can click any:
$("#accordion").find("span.rght").text("▼");
if($(this).find("span.rght").text()=="▼"){
$(this).find("span.rght").text("▲");
}
//else{$(this).find("span.rght").text("▼");} //sometime does not work!?
});
});
//see working website: http://evadapter.com/faq.html
<style>
.rght{float:right}
</style>
我放弃使用手风琴是因为我的一些客户要求比较 2 个答案,但主要是因为使用我测试了 1000 个问题的简单 HTML5 详细信息控件的效率! 在我的新客户估算的单个常见问题页面中。 Accordion 的问题从 140 项开始,参见https://github.com/twbs/bootstrap/issues/26419
这是最简单有效的解决方案,具有完全控制和自动“全部折叠”按钮的出现和消失。 如果你想在真实网站上看到更高级的实现: https : //airtempheating.com/faq
<details>
<summary>
Is there any advantage to setting the thermostat fan setting to “On” or “Auto” mode all the time?</summary>
Yes! You will have constant filtering of the air. A second advantage is that the constant airflow will allow an even temperature throughout your home.
However, if your home feels very humid, set the fan to the “Auto” mode.
</details>
<details>
<summary>
How long does a typical furnace or air conditioner last?</summary>
New air conditioning and heating equipment lasts longer than ever! The end of a furnace's or air conditioner’s service life depends on more than just chronological age.
Energy-efficiency issues and the price of any necessary repairs versus the cost of upgrading to a new unit all enter into that determination.
</details> <hr>
<button type="button" id="hdn" class="btn btn-primary" onClick="window.location.reload();">Collapse All</button>
<style>
#hdn{display:none; visibility:visible}
</style>
<script>
$(function() {$('summary').click(function() {if($('#hdn').css("display") == "none"){$('#hdn').show();}});});
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.