create style element, define some classes and add class to element
is there any better way?
$("< style >").attr("t开发者_运维知识库ype","text/css").prependTo("head").append(".bor {border:2px dotted red} .gto {padding:10px; font-size:medium}");
$("input:text").addClass("bor gto").val("your text");
http://jsfiddle.net/adatepe/aLmc2/
Maybe you could set the CSS directly rather than trying to build CSS classes at runtime:
$('input:text').css({
border: '2px dotted red',
padding: '10px',
fontSize: 'medium'
}).val('your text');
If by any chance you need to do this way why not do it the simple way then?
$("<style type='text/css'>.bor {border:2px dotted red}" +
" .gto {padding:10px; font-size:medium}</style>").prependTo("head");
精彩评论