有没有办法切换.append()jQuery方法

Is there a way to toggle the .append() jQuery method?

本文关键字:jQuery 方法 append 有没有      更新时间:2023-09-26

我想用appendToggle另一个img来代替plus.svg。如果你们知道如何切换.append()方法,那就太好了。我想在你点击CSS时更改它,然后再更改一次,基本上。

           $(function(){
            $("#btw").click(function(){
                $("#btwl").slideToggle(300);
                $(this).append("<style>#btw::before{content: url(minus.svg);width: 16px;eight: 16px;background-size: 16px 16px;background-repeat: no-repeat;vertical-align: middle;padding-right: 10px;}</style>");
            });
        });

如果使用toggleClass:,也可以达到同样的效果

$(this).toggleClass('selected');

然后在CSS样式表中执行以下操作:

.selected::before {
    content: url(minus.svg);
    width: 16px;
    height: 16px;
    background-size: 16px 16px;
    background-repeat: no-repeat;
    vertical-align: middle;
    padding-right: 10px;
}

来源:http://api.jquery.com/toggleClass/

您可以通过向样式提供id来删除css

$(function(){
    $("#btw").click(function(){
        $("#btwl").slideToggle(300);
        if($('#btwl').is(':hidden')) {
            $(this).append("<style id='toggle_css'>#btw::before{content: url(minus.svg);width: 16px;eight: 16px;background-size: 16px 16px;background-repeat: no-repeat;vertical-align: middle;padding-right: 10px;}</style>");
        } else {
            $('#toggle_css').remove();
        }
    });
});

您可以使用toggleClass
将所需的样式放在某个类下并进行

   $("#btw").click(function(){
       $(this).toggleClass("requiredClass");
   });

使用toggleClass方法:

<style style="text/css">
    .before::before{
        content: url(minus.svg);
        width: 16px;
        height: 16px;
        background-size: 16px 16px;
        background-repeat: no-repeat;
        vertical-align: middle;
        padding-right: 10px;
    }
</style>
<script type="text/javascript">
    $(function(){
        $("#btw").click(function(){
            $("#btwl").slideToggle(300);
            $(this).toggleClass("before");
        });
    });
</script>