如何在jQuery中编辑css规则? [英] How can I edit a css rule in jQuery?
问题描述
目的是将行动态添加到表中,并且稍后手动应用规则也不太好(而且速度较慢)。
The purpose is that rows are dynamically added to a table and it is not as nice (and slower) to apply the rule manually later.
具体示例是我创建一个树表来表示文件夹目录。每个文件夹都是 div
。在每个 div
中,有一个 ul
和 li
为每列的信息。这些 li
有一个类名等同于列名。这提供了列宽。我想让列可调整大小。我可以做 $('。className')。css('width',newWidth)
但是这不会应用于新插入的项目。因此,我想修改css规则。
The specific example is that I am creating a tree table to represent a folder directory. Each folder is a div
. In each div
, there is a ul
with an li
for each columns' information. These li
have a class name equivalent to the column name. This provides column width. I want to make the columns resizable however. I could do $('.className').css('width', newWidth)
but then this wont apply to newly inserted items. Therefore, I want to modify the css rule. How do I do this?
推荐答案
jQuery不是真的以这种方式编辑CSS。
jQuery isn't really for editing CSS in that way.
可以使用 .css()
来实现你想要的。不要更改单个元素的宽度,将它们保持为100%,并调整其父容器的大小,因此任何新插入的项目的宽度都为100%,并采用父容器的宽度。
It's possible to achieve what you want using .css()
though. Don't change the width of individual elements, keep them 100% and resize their parent container, so any newly inserted items will be of width 100% and take the width of the parent container.
EDIT
如何动态插入/更新行?如果您使用 .clone()
现有行,应包括 .css()
添加的行内css,这意味着您不必调整大小。
EDIT
How are you dynamically inserting/updating rows? If you use .clone()
on an existing row, the in-line css added by .css()
should be included, meaning you won't have to resize.
这篇关于如何在jQuery中编辑css规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!