Why the JQuery syntax below is not correct:

 $('#Footer').css({ right: 6%, bottom: 0 });  

And this one is:

 $('#Footer').css('right', '6%');
 $('#Footer').css('bottom', '0');

What is incorrect in the first code ?

Thanks in advance

4 Answers 正确答案

6% is not a number, so it must be specified as a string.

$('#Footer').css({ right: '6%', bottom: 0 });  

you are missing ''

$('#Footer').css({ right: '6%', bottom: '0' });  

for reference http://api.jquery.com/Types/#jQuery

because of your missing quotes

it should be

 $('#Footer').css({ 'right': '6%', 'bottom': 0 }); 

$('#Footer').css({ 'right': '6%', 'bottom': 0 });

Is the right answer as Kimtho6 stated.

Kapa is universally mistaken in this instance. As the quotes are only optional when using single word properties; if you used his reasoning then your code would fail when getting the padding-right property.

It's the same reason JSON requires quotes. Because quotes make property names universal. And because that's how JS and many other coding languages do not allow the - character as standalone property names; because it was a math symbol before there was any computer coding languages.

Fail:

$('#Footer').css({ padding-top: '6%', padding-bottom: 0 });

Success:

$('#Footer').css({ 'padding-top': '6%', 'padding-bottom': 0 });

  • You are universally mistaken, because if you write 56vw instead of 0 your code will fail :). – kapa Oct 6 '15 at 7:53

来自  https://stackoverflow.com/questions/7607871/percentage-in-jquery-css-positioning