Javascript 如何使用 jQuery 设置溢出-x?或者:如何使用“-”(破折号)设置 css-properties?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6306813/
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-08-23 21:12:23  来源:igfitidea点击:

How to set overflow-x with jQuery? Or: How to set css-properties with a '-' (dash)?

javascripthtmleventsdom

提问by Kees C. Bakker

I would like to set the overflow-xcss property with jQuery, but it doesn't work:

我想overflow-x用 jQuery设置css 属性,但它不起作用:

$('.mySelector').css({
    color: 'Red',
    overflow: 'auto',
    overflow-x: 'scroll'
});

How would I set properties that have a '-' (dash) in them?

我将如何设置带有“-”(破折号)的属性?

回答by Michael Robinson

You could use either camel-case:

您可以使用任何一种驼峰式:

$('.mySelector').css({
    color: 'Red',
    overflow: 'auto',
    overflowX: 'scroll'
});

Or quoted keys:

或引用的键:

$('.mySelector').css({
    color: 'Red',
    overflow: 'auto',
    'overflow-x': 'scroll'
});

I would recommend quoted keys, I find it easier to tell 'at a glance' what a rule is compared to camel-casing.

我会推荐带引号的键,我发现“一目了然”更容易分辨出规则与驼峰式外壳相比。

回答by alex

You can quote it (with single or double quotes) or use the camel case variant (which the DOM adopted with its styleobject, because -are not legal in identifiers).

您可以引用它(使用单引号或双引号)或使用驼峰式大小写变体(DOM 对其style对象采用这种变体,因为-标识符不合法)。

$('.mySelector').css({
    'color': 'Red',
    'overflow': 'auto',
    'overflow-x': 'scroll'
});