单击时切换div文本

时间:2015-09-08 14:09:44

标签: javascript jquery html

这是我的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')也需要更改。我已尝试使用<Location /> AuthType Basic AuthName "Enter Login and Password to Enter" AuthUserFile /home/content/html/.htpasswd <If "%{HTTP_USER_AGENT} == 'myuseragent'"> Require all granted </If> <Else> Require valid-user Require ip 12.34.567.89 </Else> </Location> 进行此操作,但无法找到正确的元素

6 个答案:

答案 0 :(得分:6)

这里是fiddle。这是你在找什么?

这是我添加的代码。

if($(this).find("span#accordionIcon").text()=="▼"){
    $(this).find("span#accordionIcon").text("▲");
}
else{
    $(this).find("span#accordionIcon").text("▼");
}

答案 1 :(得分:2)

接受的答案仅适用于一个切换。 这是版本(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('▼');
        }
    });
});

答案 2 :(得分:1)

或者在不更改代码的情况下,您可以这样做:

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");
    });
});

JSFIDDLE

答案 3 :(得分:0)

如果你想使用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>

答案 4 :(得分:0)

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>

答案 5 :(得分:0)

我之所以选择使用Accordion,是因为我的一些客户要求比较2个答案,但是主要是因为使用我测试了1000个问题的简单HTML5 Details控件的效率!在一个常见问题解答页面中,可以估算出我的新客户。手风琴的问题始于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>