开发者

percentage in jquery css Positioning

开发者 https://www.devze.com 2023-04-09 19:45 出处:网络
Why the JQuery syntax below is not correct: $(\'#Footer\').css({ right: 6%, bottom: 0 }); And this one is:

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


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 });

0

精彩评论

暂无评论...
验证码 换一张
取 消