添加内容到活动元素,但如果元素不活动则删除它?

时间:2015-01-02 16:09:29

标签: jquery html

我有一个菜单,单击它会在列表项(段落)中添加.active类和一些内容。我已经想出了如何添加活动类,但是一旦列表项不再具有活动类,我不确定如何删除添加内容。

这是我的HTML:

<ul>
    <li><a href="#nowhere" title="Lorum ipsum dolor sit amet">Lorem</a></li>
    <li><a href="#nowhere" title="Aliquam tincidunt mauris eu risus">Aliquam</a></li>
    <li><a href="#nowhere" title="Morbi in sem quis dui placerat ornare">Morbi</a></li>
    <li><a href="#nowhere" title="Praesent dapibus, neque id cursus faucibus">Praesent</a></li>
    <li><a href="#nowhere" title="Pellentesque fermentum dolor">Pellentesque</a></li>
</ul>

我的jQuery:

$("ul li").click(function(){
    $(this).addClass("active").siblings().removeClass("active");
        $(this).append(function(){
            if ($(this).hasClass("active")) {
                return "<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
            } else {
                return false;
            }
        });
});

这会在添加.active时添加所需的段落,但一旦列表项不再具有此类,则不会删除它。有什么想法吗?

2 个答案:

答案 0 :(得分:2)

您永远不会从任何其他p中删除li - 而且您也不需要进行冗余检查:

$("ul li").click(function(){
    //Remove active class and remove p
    $("ul li.active").removeClass("active").each(function() {
        $(this).find("p").remove(); //remove old p
    });

    //Add active class to element clicked and append
    $(this).addClass("active").append(function(){
        return "<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
    });
});

演示:http://jsfiddle.net/8ufx60ry/

答案 1 :(得分:0)

不太理想,但使用您当前的设置,您可以轻松完成此操作:

    $("ul li").on('click', function(e){
        if($(this).is(':not(.active)')) {
            $(this).addClass("active").append(function(){
                return "<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
            }).siblings().removeClass("active").find('p').remove();
        }
        
        // for demo purposes
        e.stopPropagation();
    });
ul {
    list-style-type: none;
    padding: 0;
    margin: 2em;
}

p {
    margin: 0;
}

a {
    color: #C55;
    text-decoration: none;
}

li.active a {
    color: #55C;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/normalize/3.0.2/normalize.css" rel="stylesheet"/>
<ul>
    <li><a href="#nowhere" title="Lorum ipsum dolor sit amet">Lorem</a></li>
    <li><a href="#nowhere" title="Aliquam tincidunt mauris eu risus">Aliquam</a></li>
    <li><a href="#nowhere" title="Morbi in sem quis dui placerat ornare">Morbi</a></li>
    <li><a href="#nowhere" title="Praesent dapibus, neque id cursus faucibus">Praesent</a></li>
    <li><a href="#nowhere" title="Pellentesque fermentum dolor">Pellentesque</a></li>
</ul>