jsÖÐdocumentÊÇʲôÒâ˼
document¹¤¾ß´ú±íÄ¿½ñ¼ÓÔصÄhtmlÎĵµ£¬ÌṩҪÁìºÍÊôÐÔÀ´ÓëÒ³ÃæÔªËؽ»»¥¡¢»ñÊØÐÅÏ¢ºÍÐÞ¸ÄÄÚÈÝ¡£Òªº¦ÒªÁìºÍÊôÐÔ°üÀ¨£ºgetelementbyid£¨°´id»ñÈ¡ÔªËØ£©¡¢createelement£¨½¨ÉèÔªËØ£©¡¢appendchild£¨×·¼ÓÔªËØ£©¡¢body£¨ÎĵµÄÚÈݲ¿·Ö£©¡¢documentelement£¨htmlÎĵµ¸ùÔªËØ£©¡¢title£¨»ñÈ¡/ÉèÖÃÎÊÌ⣩¡¢url£¨»ñÈ¡Îĵµurl£©¡£
DOMÖеÄÎĵµ¹¤¾ß
Question: jsÖÐdocumentÊÇʲôÒâ˼£¿
Answer:
document¹¤¾ßÔÚJavaScriptÖÐÌåÏÖÄ¿½ñ¼ÓÔصÄHTMLÎĵµ¡£ËüÌṩÁËһϵÁÐÒªÁìºÍÊôÐÔ£¬ÔÊÐí¾ç±¾ÓëÒ³ÃæÖеÄÔªËؾÙÐн»»¥¡¢»ñÊØÐÅÏ¢ºÍÐÞ¸ÄÎĵµÄÚÈÝ¡£
Ïêϸ½â´ð:
document¹¤¾ßÊÇÎĵµ¹¤¾ßÄ£×Ó(DOM)µÄ¸ù½Úµã¡£DOMÊÇÒ»¸öÓÃÓÚÌåÏֺͲÙ×÷HTMLºÍXMLÎĵµµÄÊ÷×´Êý¾Ý½á¹¹¡£document¹¤¾ß³äµ±DOMÊ÷µÄ¸ù½Úµã£¬°üÀ¨Õû¸öÒ³ÃæµÄ½á¹¹ºÍÄÚÈÝ¡£
Òªº¦ÒªÁìºÍÊôÐÔ:
getElementById(): ƾ֤ID»ñÈ¡Ò³ÃæÔªËØ¡£
getElementsByTagName(): ƾ֤±êÊðÃû»ñÈ¡Ò³ÃæÔªËصÄÁÐ±í¡£
createElement(): ½¨ÉèеÄHTMLÔªËØ¡£
appendChild(): ½«Ò»¸öÔªËظ½¼Óµ½ÁíÒ»¸öÔªËصÄĩβ¡£
body: Ö¸ÏòÎĵµµÄÄÚÈݲ¿·Ö¡£
documentElement: Ö¸ÏòHTMLÎĵµÔªËØ¡£
title: »ñÈ¡»òÉèÖÃÎĵµµÄÎÊÌâ¡£
URL: »ñÈ¡ÎĵµµÄURL¡£
ʹÓÃʾÀý:
// »ñÈ¡ÎĵµÎÊÌâ let title = document.title; // »ñÈ¡´øÓÐID "main" µÄÔªËØ let mainElement = document.getElementById("main"); // ½¨ÉèÒ»¸öеĶÎÂäÔªËØ let newParagraph = document.createElement("p"); newParagraph.textContent = "This is a new paragraph"; // ½«Ð¶ÎÂäÌí¼Óµ½ÎĵµÖ÷Ìå document.body.appendChild(newParagraph);
µÇ¼ºó¸´ÖÆ
ͨ¹ýʹÓÃdocument¹¤¾ß£¬JavaScript¾ç±¾¿ÉÒÔ¶¯Ì¬µØÐÞ¸ÄÒ³ÃæÄÚÈÝ¡¢ÏìÓ¦Óû§½»»¥²¢ÓëÆäËûÔªËؾÙÐÐͨѶ£¬´Ó¶ø½¨Éè¸ü½»»¥ºÍÏìÓ¦µÄÍøÒ³ÌåÑé¡£
ÒÔÉϾÍÊÇjsÖÐdocumentÊÇʲôÒâ˼µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡