jquery if else条件为css api [英] jquery if else condition for css api

查看:136
本文介绍了jquery if else条件为css api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下jquery语句

I have the below jquery statement

$(this).('span.section1').css('background','url("images/accordion_closed_left.png") no-repeat scroll 0 0 transparent');

如何为css条件编写if else语句。我的意思是如果背景图像是accordion_closed_left.png然后<做这个> 或者<做这个>

How do I write a if else statement for the css condition. What I mean is if the background image is accordion_closed_left.png then <do this> or else <do this>.

准确无误<做这个> 是声明块。

感谢您的时间。

推荐答案

我建议改用 css类,然后调用jQuery的 .hasClass()方法:

I'd recommend to use a css class instead and then call jQuery's .hasClass() method on it:

css

.closed {
    background: url("images/accordion_closed_left.png") no-repeat scroll 0 0 transparent;
}

js

var $target = $(this).find('span.section1');

if( $target.hasClass( 'closed' ) ) {
    $target.removeClass( 'closed' );
} 
else {
    $target.addClass( 'closed' );
}

这篇关于jquery if else条件为css api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆