javascript - 需要帮助将数据插入 html
问题描述
我想将 Javascript 变量的值插入到 html 文件中。但是输出是空白的。所需的数据存储在变量中,但 html 文件中的输出为空白。
我的htmll文件
<div class="student-info">
<p>Name: Anon</p>
<p>Registration Number:</p>
<script src="resources/js/data_inp.js">document.write(regno)</script>
<script src="resources/js/data_inp.js"></script>
我的 data_inp.js 文件
//get regno
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
console.log(user);
var email = firebase.auth().currentUser.email;
var regno = email.substring(0, email.length - 8);
//email is eg. 1234@xyz.com where 1234 is regno so I used this method to separate the regno
console.log(regno);
}
});
我究竟做错了什么?
解决方案
在您的 HTML 中删除script
并使用 a 制作 a span
,id
然后innerText
以 span 为目标。
<p>Registration Number: <span id="dynamic-value"></span></p>
然后在你的js中,
document.getElementById('dynamic-value').innerText = regno
推荐阅读
- python - 如何在定义函数时将随机数数组传递给 random.choice?
- csv - Unix 邮件正文中 CSV 文件内容的格式不正确
- python - Mongo - 从特定属性的元组列表中获取最大值
- python - Python 脚本执行失败并在 Windows 上使用 sys.argv
- c++ - C++ 从基类指针转换派生类指针
- google-apps-script - UrlFetchApp Google Apps 脚本 IP 范围的更新列表?
- javascript - 如何使用javascript将参数传递给方法?
- google-sheets - 如果另一列中的任何单元格为空,则突出显示所有重复的单元格
- javascript - native-base 是否有一个实用方法可以让我们提取计算的主题数据?
- nginx - 如果响应 NGINX 中没有 Cache-Control 标头,如何缓存内容