【问题标题】:JQuery Toggle Class Plus and minus icon issueJQuery切换类加号和减号图标问题
【发布时间】:2016-01-30 12:14:37
【问题描述】:

我有两个 div 与流行和其他,我正在使用 jquery toggleClass 在两个 div 部分之间的 slideUp 和 Slidedown 目前它工作正常但是当我点击流行 div 时它显示流行内容的内容但没有改变它的图标减号加号减号反之亦然,这两个 div 都可以有人帮我解决这个问题谢谢!

http://jsfiddle.net/v9Evw/352/

HTML

<div id="popular">

    <div id="headerDivImg1" class="toggle1" id="popular_img" style='background:#4c97d0;height:30px;width:640px;'>
        <span style='color:#fff;padding-left:10px;line-height:30px;'>DIV1</span>
        <a class="toggle1" style='float:right;' id="popular_img" href="javascript:popular('popular_content', 'popular_img');">

        </a>
    </div>
    <div id="popular_content" class="content" style="display: block;">
        <p>welcome to Div1</p>
    </div>

</div>

<br/>

<div id="others">

    <div id="headerDivImg1" id="other_img" class="toggle2" style='background:#4c97d0;height:30px;width:640px;'>
        <span style='color:#fff;padding-left:10px;line-height:30px;'>DIV2</span>
        <a class="toggle2" style='float:right;' id="other_img" href="javascript:popular('popular_content', 'popular_img');">

        </a>
    </div>
    <div id="other_content" class="content" style="display: block;">
        <p>welcome to Div2</p>
    </div>

</div>

JQUERY

$(document).ready(function()
{
    var $content = $("#popular_content").show();
    $(".toggle1").on("click", function(e)
    {
        $(this).toggleClass("expanded");
        $content.slideToggle();
    });
});

$(document).ready(function()
{
    var $content = $("#other_content").hide();
    $(".toggle2").on("click", function(e)
    {
        $(this).toggleClass("expanded");
        $content.slideToggle();
    });
});

CSS

.toggle1
{
    display:inline-block;
    height:27px;
    width:41px;  background:url("minus.png");

}
.toggle1.expanded
{
    background:url("plus.png");
}


.toggle2
{
    display:inline-block;
    height:27px;
    width:41px;  background:url("plus.png");

}
.toggle2.expanded
{
    background:url("minus.png");
}

【问题讨论】:

    标签: javascript jquery html css


    【解决方案1】:

    我认为这可能会有所帮助

    HTML

        <div class="div1container">
            <div class="glyphicon glyphicon-plus divcontainer" data-toggle="collapse" data-target="#demo">Div1</div>
    
        <div id="demo" class="collapse">
        Div 1 Content
        </div>
        </div>
        <div class="div2container">
        <div class="glyphicon glyphicon-plus divcontainer" data-toggle="collapse" data-target="#demo2">Div2</div>
    
        <div id="demo2" class="collapse">
        Div 2 Content
        </div>
            </div>
    

    CSS

    .divcontainer{
        background:aqua;
        width:100%
    }
    

    jQuery

    $(document).ready(function () {
        $('.glyphicon-plus').click(function () {
            $(this).parent("div").find(".glyphicon-plus")
                .toggleClass("glyphicon-minus");
        });
    });
    

    【讨论】:

    • 非常感谢 Raviteja...您的编码工作出色只需一个小请求,您就可以在 div1container 类中将加号/减号放在右侧,并将 div 名称放在左侧,目前两者都在左侧 谢谢!!!!
    猜你喜欢
    • 2016-01-30
    • 2019-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多