jsÖÐÔõÑùÉèÖÃ×ÖÌå
ÔõÑùÔÚ javascript ÖÐÉèÖÃ×ÖÌ壿ʹÓà css ÒªÁ죺ÄÚÁªÑùʽ£ºdocument.getelementbyid(“myelement”).style.fontfamily = “arial”;css ÀࣺʹÓà css ÀàÀ´ÉèÖÃ×ÖÌåÑùʽ¡£css ¹¤¾ß£ºÊ¹Óà javascript ¹¤¾ßÀ´ÉèÖÃ×ÖÌåÑùʽ¡£cssom£ºÊ¹Óà cssom ½Ó¿ÚÐÞ¸Ä css Ñùʽ¡£javascript ¿â£ºÊ¹Óà jquery µÈ¿âÇáËÉÉèÖÃ×ÖÌåÑùʽ¡£
ÔõÑùÔÚ JavaScript ÖÐÉèÖÃ×ÖÌå
ÔÚ JavaScript ÖУ¬¿ÉÒÔͨ¹ý CSS ÑùʽÀ´ÉèÖÃ×ÖÌå¡£ÓÐÒÔϼ¸ÖÖÒªÁ죺
1. ʹÓÃÄÚÁªÑùʽ
document.getElementById("myElement").style.fontFamily = "Arial";
µÇ¼ºó¸´ÖÆ
2. ʹÓà CSS Àà
ÔÚ HTML Öн¨ÉèÒ»¸ö CSS À࣬ȻºóʹÓøÃÀàÀ´ÉèÖÃ×ÖÌå¡£
<style> .myClass { font-family: Arial; } </style>
µÇ¼ºó¸´ÖÆ
document.getElementById("myElement").classList.add("myClass");
µÇ¼ºó¸´ÖÆ
3. ʹÓà CSS ¹¤¾ß
¿ÉÒÔʹÓÃÒÔÏ JavaScript ¹¤¾ßÀ´ÉèÖÃ×ÖÌåÑùʽ£º
var myElement = document.getElementById("myElement"); myElement.style.fontSize = "16px"; myElement.style.fontFamily = "Arial";
µÇ¼ºó¸´ÖÆ
4. ʹÓà CSSOM
CSSOM ÊÇÒ»¸ö DOM ½Ó¿Ú£¬ÔÊÐíÄãÐÞ¸Ä CSS Ñùʽ¡£ÒÔÏÂÊÇÔõÑùʹÓÃËüÀ´ÉèÖÃ×ÖÌ壺
var styleSheet = document.styleSheets[0]; var rule = styleSheet.cssRules[0]; rule.style.fontFamily = "Arial";
µÇ¼ºó¸´ÖÆ
5. ʹÓà JavaScript ¿â
¿ÉÒÔʹÓà JavaScript ¿â£¨Èç jQuery£©À´ÇáËɵØÉèÖÃ×ÖÌåÑùʽ¡£
$("#myElement").css("font-family", "Arial");
µÇ¼ºó¸´ÖÆ
ÐèҪעÖصÄÊÇ£¬²î±ðµÄä¯ÀÀÆ÷¿ÉÄÜÖ§³Ö²î±ðµÄ×ÖÌåÉèÖÃÒªÁì¡£Òò´Ë£¬½¨ÒéÔÚÄãµÄ´úÂëÖÐʹÓÿçä¯ÀÀÆ÷µÄ½â¾ö¼Æ»®¡£
ÒÔÉϾÍÊÇjsÖÐÔõÑùÉèÖÃ×ÖÌåµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡