如何在现有元素中动态插入具有某种格式的元素 ( <ui>
)?
例如:
$("#contentDiv").append('<ui id="contentUi">');
$("#contentUi").css({ "text-align": "center", "list-style-type": "none"});
$("#contentDiv").css({ "text-decoration": "none","font-size":" 1.30em","font-weight":
"bold"});
在这里#contentDiv
是元素的id <div id="contentDiv">
,我正在附加一个新元素 <ui>
第 3 行运行良好,但第 2 行对屏幕没有影响。
最佳答案
我认为这里没有必要用 jQuery 定义 css。只需在您的样式表中预定义一个类,这样就更简单了。
举个例子。
$("#contentDiv").append('<ui class="predefined-class">');
$("#contentDiv").addClass('another-predefined-class');
在你的 CSS 中你会有
.predefined-class {
text-align: center;
list-style-type: none;
}
.another-predefined-class {
text-decoration:none;
font-size: 1.30em;
font-weight: bold;
}
我希望这就是您想要的。
我也不知道你附加一个空的 ul
有多好,因为没有 li
s
关于javascript - 如何将 CSS 与动态插入的元素一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6622843/