我正在尝试使用 javascript 使用两个单独的外部 js 文件比较两个 HTML 页面中的文本,并导入要比较的所需变量。但是该函数没有显示任何结果。
我的第一个 html 是:
<!DOCTYPE html>
<html>
<body>
<span id='current-name'>John Hamish Smith</span>
<p id='new-billing'>0</p>
<script src='experiment.js' type="text/javascript"></script>
</body>
</html>
下面的实验.js:
const currentName = document.getElementById('current-name').textContent;
const newBillingField = document.getElementById('new-billing');
const currBillingValue = Number(newBillingField.textContent);
let newBillingValue = currBillingValue + 1;
import {nameCheck} from './test2';
const testing = () => {
if(currentName === nameCheck) {
newBillingField.textContent = newBillingValue;
alert('hi there');
}
}
window.addEventListener('load', testing);
那么我的第二个 html 就是:
<!DOCTYPE html>
<html>
<body>
<p id='userName'>John Hamish Smith</p>
<script src='./test2.js' type='text.javascript'></script>
</body>
</html>
和test2.js如下:
let nameCheck = document.getElementById('userName').textContent;
export {nameCheck}
然而,即使名称相同,我的 newBillingValue 仍然是 0。 (提醒只是为了方便功能测试)
谢谢!