1

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

4 回答 4

2

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

于 2013-07-17T05:26:48.457 回答
2

您可以通过向样式提供 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();
        }
    });
});
于 2013-07-17T05:26:58.713 回答
1

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

   $("#btw").click(function(){
       $(this).toggleClass("requiredClass");
   });
于 2013-07-17T05:24:58.217 回答
1

使用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>
于 2013-07-17T05:26:32.430 回答