繁体   English   中英

多个HTML文件连接到一个JS文件?

[英]Multiple HTML files connected to one JS file?

因此,如果我有多个HTML文件连接到一个JS文件,那么当我运行使用document关键字的代码时,该代码是否知道我要引用的文档? 例如,在其中一个HTML文件中,我在body标签和js文件中都有一个ID为main的div,我创建了一个新的div以附加到该特定的html文件。 js文件中的代码基本上是var div = document.createElement(“ div”),后跟document.getElementById(“ main”)。appendChild(div)。 我的问题基本上是,当我使用getelementbyid时,代码是否知道将div附加到哪个html文件以及要使用哪个html文件? 如果没有,如果我有多个html文件连接到一个js文件,该如何指定?

只要您指定要更改的div的ID,它就可以工作。

js可能像这样:

var div1 = document.createElement("div")
document.getElementById("id1").appendChild(div1); //matches to div1 in the html

var div2 = document.createElement("div");
document.getElementById("id2").appendChild(div2).  //matches to div2 in the html

var div3 = document.createElement("div")
document.getElementById("id3").appendChild(div3).  //matches to div3 in the html

的HTML可能是这样的:

<div id="id1"></div>

<div id="id2"></div>

<div id="id3"></div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM