有没有更好的办法?
$("< style >").attr("type","text/css").prependTo("head").append(".bor {border:2px dotted red} .gto {padding:10px; font-size:medium}");
$("input:text").addClass("bor gto").val("your text");
最佳答案
也许你可以 set the CSS directly而不是尝试在运行时构建 CSS 类:
$('input:text').css({
border: '2px dotted red',
padding: '10px',
fontSize: 'medium'
}).val('your text');
关于javascript - 创建样式元素,定义一些类并将类添加到元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5781307/