javascript - jquery if else condition for css api - Stack Overflow

I have the below jquery statement $(this).('span.section1').css('background','

I have the below jquery statement

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

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>.

just to be precise <do this> are blocks of statement.

Thanks for your time.

I have the below jquery statement

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

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>.

just to be precise <do this> are blocks of statement.

Thanks for your time.

Share Improve this question edited May 23, 2011 at 23:56 Felix Kling 818k181 gold badges1.1k silver badges1.2k bronze badges asked May 23, 2011 at 23:18 JayJay 3131 gold badge5 silver badges11 bronze badges 2
  • This is going to be slow and horrendous to maintian. Use classes instead. – jwueller Commented May 23, 2011 at 23:24
  • This is surely not correct: $(this).('span.section1') – Felix Kling Commented May 23, 2011 at 23:57
Add a ment  | 

3 Answers 3

Reset to default 8

I'd remend 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' );
}

How to write this in jQuery with 'guides' as a class name....

<script type="text/javascript" >
function guideMenu()
{
    if (document.getElementById('guides').style.display == "none") {
        document.getElementById('guides').style.display = "block";
    }
    else {
        document.getElementById('guides').style.display = "none";
    }
}
</script>

To naively answer your question:

if ($(this).('span.section1').css('background').match('|/accordion_closed_left\.png"|')) {

}
else {

}

However I support jAndy's answer, .hasClass() is the way to go!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745438404a4627716.html

相关推荐

  • javascript - jquery if else condition for css api - Stack Overflow

    I have the below jquery statement $(this).('span.section1').css('background','

    9小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信