jsÔõôɾ³ý±êÇ©
javascript ÌṩÁËÒÔÏÂÒªÁìɾ³ý html ±êÇ©£ºÊ¹Óà remove() ÒªÁ죬ֱ½Ó´Ó dom ÖÐɾ³ý±êÇ©¡£Ê¹Óà parentnode.removechild() ÒªÁ죬ͨ¹ý¸¸½Úµãɾ³ý±êÇ©¡£Ê¹Óà innerhtml ÊôÐÔ£¬½«±êÇ©µÄ html ÄÚÈÝÌ滻Ϊ¿Õ×Ö·û´®£¬´Ó¶øÒƳý±êÇ©¡£
ÔõÑùÔÚ JavaScript ÖÐɾ³ý±êÇ©
JavaScript ÌṩÁ˼¸ÖÖÒªÁìÀ´É¾³ý HTML ÎĵµÖеıêÇ©£º
1. ʹÓà remove() ÒªÁì
document.querySelector("±êÇ©Ñ¡ÔñÆ÷").remove();
µÇ¼ºó¸´ÖÆ
´ËÒªÁìÖ±½Ó´Ó DOM ÖÐɾ³ýÖ¸¶¨µÄ±êÇ©¡£
2. ʹÓà parentNode.removeChild() ÒªÁì
let target = document.querySelector("±êÇ©Ñ¡ÔñÆ÷"); target.parentNode.removeChild(target);
µÇ¼ºó¸´ÖÆ
´ËÒªÁìÒ²´Ó DOM ÖÐɾ³ýÖ¸¶¨µÄ±êÇ©£¬µ«Ëüͨ¹ýÆ丸½Úµã¡£
3. ʹÓà innerHTML ÊôÐÔ
document.querySelector("±êÇ©Ñ¡ÔñÆ÷").innerHTML = "";
µÇ¼ºó¸´ÖÆ
´ËÒªÁìÌæ»»±êÇ©µÄ HTML ÄÚÈÝΪ¿Õ×Ö·û´®£¬ÓÐÓõØɾ³ý±êÇ©¡£
ʾÀý£º
Ҫɾ³ý¾ßÓÐ id Ϊ “my-tag” µÄ±êÇ©£¬¿ÉÒÔʹÓÃÒÔÏ´úÂ룺
document.querySelector("#my-tag").remove();
µÇ¼ºó¸´ÖÆ
Ҫɾ³ý¾ßÓÐ class Ϊ “my-class” µÄËùÓбêÇ©£¬¿ÉÒÔʹÓÃÒÔÏ´úÂ룺
document.querySelectorAll(".my-class").forEach(element => { element.remove(); });
µÇ¼ºó¸´ÖÆ
×¢ÖØ£º
ʹÓà remove() ÒªÁìʱ£¬×Ó½ÚµãÒ²»á±»É¾³ý¡£
ÈôÊÇҪɾ³ý±êÇ©¼°ÆäÄÚÈÝ£¬ÇëʹÓà innerHTML ÊôÐÔ¡£
È·±£ÔÚʹÓà parentNode.removeChild() ÒªÁì֮ǰÏÈ»ñÈ¡±êÇ©µÄÒýÓá£
ÒÔÉϾÍÊÇjsÔõôɾ³ý±êÇ©µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡