Domanda

I have div that makes something by default and makes something else if it has a specific width. I think I have the problem in the conditional. How can I say: "if you have width 150px do something but do not do anything else" What is wrong with my code?

Here is the example to play Fiddle

jQUERY:

   $(function(){
        $('#menu').click(function(event) {
        if ('#menu'.width() === 150){
        event.preventDefault();
            alert("something");         
        }
        }); 

        $("#menu").click(function() {
        $("this").animate({right: 0}, 500);
        });     
    })

CSS:

#menu {
    position:absolute;
    top:50px; right:50px;
    width:150px; height:108px;
    background:blue;
    cursor:pointer;
}

HTML:

<div id="menu"> </div>
È stato utile?

Soluzione

I have made a change to your syntax - http://jsfiddle.net/w25mQ/4/

if( $('#menu').width === 150 )....

and

$(this).animate....

The commenter is right though, you should combine your click handlers, like this - http://jsfiddle.net/w25mQ/5/

Altri suggerimenti

You have a lot of misuse with jQuery in your code :

$(function(){
    $('#menu').on('click', function(event) {
        if ($(this).width() === 150){
            event.preventDefault();
            alert("something");         
        }
    }); 

    $("#menu").on('click', function() {
        $(this).animate({right: 0}, 500);
    });
})

Here is your corrected code and the example to play with it jsfiddle

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top