首页 > 解决方案 > 需要帮助将数据插入 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);
    }
});

我究竟做错了什么?

标签: javascripthtmlfirebase

解决方案


在您的 HTML 中删除script并使用 a 制作 a spanid然后innerText以 span 为目标。

<p>Registration Number: <span id="dynamic-value"></span></p>

然后在你的js中,

document.getElementById('dynamic-value').innerText = regno

推荐阅读