简体   繁体   English

有人可以在我正在研究如何在 html 中注入 js function 的代码中找到错误吗

[英]Can someone find error in my code where I am working on how to inject js function in html

I am trying to inject js variable with html page.我正在尝试使用 html 页面注入 js 变量。 Here, I need to show givenname as "shelly" on my html page.在这里,我需要在我的 html 页面上将给定名称显示为“shelly”。 But I am not getting result from this.但我没有得到结果。 I used我用了

Hello, id="givenname" .你好, id="givenname" Let's begin.让我们开始。

to give id as "givenname". 将 id 设为“givenname”。 And in js script code I have set inner,html to get name "shelly". 在 js 脚本代码中,我设置了 inner,html 以获取名称“shelly”。 The code of js is given as document.getElementById("givenname").innerHTML="shelly"; js 的代码为 document.getElementById("givenname").innerHTML="shelly"; Can someone find what the problem is with this code. 有人可以找到此代码的问题所在。

 <.DOCTYPE html> <html lang="en"> <head> <title> Prectical2</title> <link href="styles2/style1,css" type="text/css" rel="stylesheet" /> </head> <body id="body"> <div id="content1"> <p>Hello. <b>id=givenname</b>. Let's begin?</p> </br> <div id="quiz"> <div id="N">Question N</div> <p>Question text should appear here</p> <div class="radio1"> <input type="radio" name="choices" id="A" value="A" /> <label for="A" id="choice-A">Choice A</label> </div> </br> <div class="radio2"> <input type="radio" name="choices" id="B" value="B" /> <label for="B" id="choice-B">Choice B</label> </div> </br> <div class="radio3"> <input type="radio" name="choices" id="C" value="C" /> <label for="C" id="choice-C">Choice C</label> </div> <div id="results"> <p>Your final score was.</p> </div> <div id ="quizsubmit"> <input type="button" value="Next" /> </div> </div> </div> </body> <script src="scripts/my-script.js" src="scripts/provided-script.js"> //alert(sucess) let fname="Shelly" document.getElementById("givenname");innerHTML = "fname"; </script> </html>

simply that?就这么简单?

 let fname = 'Shelly'; document.querySelector('#givenname').textContent = fname;
 <p>Hello, <b id="givenname"></b>. Let's begin.</p>

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

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