用一个JS文件检索两个不同HTML文件的DOM-JavaScript

时间:2018-10-04 09:33:25

标签: javascript html dom

我只是想在同一javascript文件上检索两个html页面的dom。

当我尝试检索第一个html页面的元素的dom时,它可以工作,但是第二个页面的元素使我返回undefined。我如何尝试检索第二页的元素?

firstpage.html

<p id="first">test first</p>

secondpage.html

<p id="second">test second</p>

file.js

var first = document.getElementById("first").innerHTML;
console.log(first); // will show me "test first"
var second = document.getElementById("second").innerHTML;
console.log(second); // will show me "undefined" .. :(

感谢您的帮助!

0 个答案:

没有答案