尊龙凯时人生就是搏

jsÔõô»ñÈ¡classÀà

ÔÚ javascript ÖлñÈ¡ÀàÃûÓÐÁ½ÖÖÒªÁ죺ʹÓà getattribute(“class”) »ñÈ¡ÀàÃûÊôÐÔÖµ¡£Ê¹Óà classlist ÊôÐÔ»ñÈ¡ domtokenlist ¹¤¾ß²¢Ê¹ÓÃÆäÒªÁì²Ù×÷Àà¡£

ÔõÑùÔÚ JS ÖлñÈ¡Àà

ÔÚ JavaScript ÖÐ £¬»ñÈ¡Ò»¸öÔªËصÄÀàÃûÓÐÁ½ÖÖÖ÷ÒªÒªÁ죺

1. ͨ¹ý getAttribute() ÒªÁì

´ËÒªÁì·µ»ØÖ¸¶¨ÔªËصÄÖ¸¶¨ÊôÐÔµÄÖµ¡£Òª»ñÈ¡ÀàµÄÊôÐÔ £¬¿ÉÒÔʹÓÃÒÔÏÂÓï·¨£º

element.getAttribute("class");

µÇ¼ºó¸´ÖÆ

ÀýÈ磺

<p id="my-paragraph" class="my-class">Hello world!</p>

µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ

const paragraph = document.getElementById("my-paragraph");
const className = paragraph.getAttribute("class");
console.log(className); // Êä³ö: "my-class"

µÇ¼ºó¸´ÖÆ

2. ͨ¹ý classList ÊôÐÔ

´ËÊôÐÔ·µ»ØÒ»¸ö DOMTokenList ¹¤¾ß £¬ÆäÖаüÀ¨ÔªËØÀàµÄÁбí¡£¿ÉÒÔʹÓÃÒÔÏÂÓï·¨»á¼û¸ÃÊôÐÔ£º

element.classList;

µÇ¼ºó¸´ÖÆ

¸Ã¹¤¾ßÌṩÁ˼¸¸öÓÐÓõÄÒªÁì £¬°üÀ¨£º

classList.add(className)£º½«Ò»¸öÐÂÀàÌí¼Óµ½ÔªËØ¡£

classList.remove(className)£º´ÓÔªËØÖÐɾ³ýÒ»¸öÀà¡£

classList.toggle(className)£ºÈôÊÇÔªËØûÓиÃÀà £¬ÔòÌí¼ÓËü £¬ÈôÊǸÃÔªËØÒÑÓµÓиÃÀà £¬Ôòɾ³ýËü¡£

classList.contains(className)£º¼ì²éÔªËØÊÇ·ñ¾ßÓÐÖ¸¶¨µÄÀà¡£

ÀýÈ磺

<p id="my-paragraph" class="my-class">Hello world!</p>

µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ

const paragraph = document.getElementById("my-paragraph");
const className = paragraph.classList;

// Ìí¼ÓÒ»¸öÐÂÀà
paragraph.classList.add("new-class");

// ´ÓÔªËØÖÐɾ³ýÒ»¸öÀà
paragraph.classList.remove("my-class");

// ¼ì²éÔªËØÊÇ·ñ¾ßÓÐÖ¸¶¨µÄÀà
if (paragraph.classList.contains("new-class")) {
  console.log("ÔªËؾßÓÐ 'new-class' Àà¡£");
}

µÇ¼ºó¸´ÖÆ

ÒÔÉϾÍÊÇjsÔõô»ñÈ¡classÀàµÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí £¬°æȨÕùÒéÓë±¾Õ¾ÎÞ¹Ø £¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í尊龙凯时人生就是搏ÂËÓÍ»úÍø¹Ù·½Ì¬¶È £¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ £¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢ £¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢ £¬ÇëÄúÁ¬Ã¦ÁªÏµ尊龙凯时人生就是搏ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ尊龙凯时人生就是搏

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎå £¬9:30-18:30 £¬½ÚãåÈÕÐÝÏ¢

QR code
sitemap¡¢ÍøÕ¾µØͼ