admin管理员组

文章数量:1426652

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!

本文标签: javascriptjquery if else condition for css apiStack Overflow