jQuery 如何编辑 element.style css?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5230319/
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
How to edit the element.style css?
提问by Haxed
Hi I am using a jquery modal pop up. See picture below.
嗨,我正在使用 jquery 模态弹出窗口。见下图。
However, the problem is, I need to edit the width. So I typed in the css file, after checking and verifying with firebug,
但是,问题是,我需要编辑宽度。所以我输入了css文件,在用firebug检查和验证后,
element.style
{
width: 700px !important;
}
But it does not work. ANy ideas as how to override the jquery css.
但它不起作用。关于如何覆盖 jquery css 的任何想法。
Some say the need to edit the css using javascript, making objects and stuff. But I'm not so sure about that.
有人说需要使用 javascript 编辑 css,制作对象和东西。但我对此不太确定。
Any help is appreciated.
任何帮助表示赞赏。
回答by balexandre
element.style
in Firebug is the style applied to the element, for exemple
element.style
在 Firebug 中是应用于元素的样式,例如
<span style="padding: 10px;">My text</span>
element.style
will have
element.style
会有
element.style
{
padding: 10px;
}
to override, the best way is to cleat the style
attribute, append a class
name and write the CSS you need.
要覆盖,最好的方法是清除style
属性,附加class
名称并编写您需要的 CSS。
Regarding your modal problem, if you are using jQuery UI Dialogcomponent, why don't you set the width programmatically, like:
关于您的模态问题,如果您使用的是jQuery UI Dialog组件,为什么不以编程方式设置宽度,例如:
$( ".selector" ).dialog({ width: 460, maxWidth: 460, minWidth: 460 });
回答by Fokko Driesprong
You will need to clear the width from the <div>
element itself. It is flagged as important so you can't override it.
您需要清除<div>
元素本身的宽度。它被标记为重要,因此您无法覆盖它。
回答by Alex
element.style is not a valid CSS selector. You would have to target the element using it's class or id, if it has one, otherwise you'd need to use traversal to target the element in either CSS, or with jQuery.
element.style 不是有效的 CSS 选择器。你必须使用它的类或 id 来定位元素,如果它有的话,否则你需要使用遍历来定位 CSS 中的元素,或者使用 jQuery。
回答by Venteens Production
Try this one, it worked for me.
试试这个,它对我有用。
If the actual width is 300px for example.
例如,如果实际宽度为 300px。
div [style^="width: 300px"]{
width:700px !important;}
This force to overwrite the element style to width 700px.
这会强制将元素样式覆盖为宽度 700 像素。