有没有一种方法来切换.append()jQuery的方法?(Is there a way to to

2019-10-18 09:20发布

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

Answer 1:

如果你使用,你可以达到同样的效果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/



Answer 2:

您可以使用toggleClass 。
将在某些阶级所需要的风格,做

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


Answer 3:

使用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>


Answer 4:

您可以通过将样式提供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();
        }
    });
});


文章来源: Is there a way to toggle the .append() jQuery method?