ÔõÑùÔÚjsÖлñÈ¡ÔªËØ
ÔõÑù»ñÈ¡ javascript ÖеÄÔªËØ£¿getelementbyid£ºÊ¹ÓÃÔªËصÄΨһ id »ñÈ¡Ìض¨ÔªËØ¡£getelementsbyclassname£ºÆ¾Ö¤ÀàÃû»ñÈ¡ËùÓÐÆ¥ÅäÔªËصÄÜöÝÍ¡£getelementsbytagname£ºÆ¾Ö¤±êÊðÃû»ñÈ¡ËùÓÐÆ¥ÅäÔªËصÄÜöÝÍ¡£queryselector£ºÊ¹Óà css Ñ¡ÔñÆ÷»ñÈ¡µÚÒ»¸öÆ¥ÅäÔªËØ¡£queryselectorall£ºÊ¹Óà css Ñ¡ÔñÆ÷»ñÈ¡ËùÓÐÆ¥ÅäÔªËصÄÜöÝÍ¡£
ÔõÑùÔÚ JavaScript ÖлñÈ¡ÔªËØ
ÔÚ JavaScript ÖУ¬¿ÉÒÔͨ¹ýÒÔÏÂÒªÁì»ñÈ¡ÔªËØ£º
1. getElementById£º
let element = document.getElementById("my-element");
µÇ¼ºó¸´ÖÆ
´ËÒªÁìƾ֤ԪËØµÄ ID »ñÈ¡ÔªËØ£¬ID ±ØÐèÊÇΨһµÄ¡£
2. getElementsByClassName£º
let elements = document.getElementsByClassName("my-class");
µÇ¼ºó¸´ÖÆ
´ËÒªÁì·µ»ØÒ»¸ö°üÀ¨ËùÓоßÓÐÖ¸¶¨ÀàÃûµÄÔªËصÄÜöÝÍ¡£
3. getElementsByTagName£º
let elements = document.getElementsByTagName("p");
µÇ¼ºó¸´ÖÆ
´ËÒªÁì·µ»ØÒ»¸ö°üÀ¨ËùÓоßÓÐÖ¸¶¨±êÊðÃûµÄÔªËصÄÜöÝÍ¡£
4. querySelector£º
let element = document.querySelector(".my-class");
µÇ¼ºó¸´ÖÆ
´ËÒªÁìƾ֤ CSS Ñ¡ÔñÆ÷»ñÈ¡µÚÒ»¸öÆ¥ÅäµÄÔªËØ¡£
5. querySelectorAll£º
let elements = document.querySelectorAll(".my-class");
µÇ¼ºó¸´ÖÆ
´ËÒªÁìƾ֤ CSS Ñ¡ÔñÆ÷·µ»ØËùÓÐÆ¥ÅäµÄÔªËصÄÜöÝÍ¡£
ÒÔÉϾÍÊÇÔõÑùÔÚjsÖлñÈ¡ÔªËصÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡