javascript jquery css 定位中的百分比

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7607871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 00:41:14  来源:igfitidea点击:

percentage in jquery css Positioning

javascriptjquerycss

提问by SilverLight

Why the JQuery syntax below is not correct:

为什么下面的 JQuery 语法不正确:

 $('#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

提前致谢

回答by kapa

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

6%不是数字,因此必须将其指定为字符串。

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

回答by Kimtho6

you are missing ''

你失踪了 ''

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

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

供参考http://api.jquery.com/Types/#jQuery

回答by Jan Dragsbaek

because of your missing quotes

因为你缺少引号

it should be

它应该是

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

回答by pixelbacon

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

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

Is the right answer as Kimtho6 stated.

是 Kimtho6 所说的正确答案。

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

在这种情况下,Kapa 是普遍错误的。由于引号仅在使用单个单词属性时是可选的;如果您使用他的推理,那么您的代码在获取padding-right属性时将失败。

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.

这与 JSON 需要引号的原因相同。因为引号使属性名称具有通用性。并且因为这就是 JS 和许多其他编码语言不允许将-字符作为独立属性名称的原因;因为在有任何计算机编码语言之前,它是一个数学符号。

Fail:

失败:

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

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

Success:

成功:

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

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