简体   繁体   English

从外部JavaScript文件添加HTML

[英]Adding html from external javascript file

I am new to javascript so please don't burn. 我是javascript新手,请不要燃烧。

I have a file Index.html which i cannot change, even a bit. 我有一个Index.html文件,我什至不能更改。

Index.html calls a javascript file experiment.js. Index.html调用了一个javascript文件Experiment.js。 What i want to do is add my html code and call a css file style.css using javascript in experiment.js. 我想做的是添加我的HTML代码,并在Experiment.js中使用javascript调用CSS文件style.css。

this is the code i am writing in experiment.js 这是我在experiment.js中编写的代码

    var link = document.createElement('link');
link.setAttribute('rel', 'stylesheet');
link.setAttribute('type', 'text/css');
link.setAttribute('href', 'style.css');
document.getElementsByTagName('head')[0].appendChild(link);

var htmlcode;
htmlcode="";
htmlcode="<div class="transbox1"> </div> <p id="heading">OHMs LAW EXPERIMENT</p>";
htmlcode=htmlcode+"<div class="transbox2"></div>";
htmlcode=htmlcode+"<p id="ohmlaw">Ohms Law states that Voltage across any resistan;e is Proportional to Current Flowing through the circuit.<br>V=IR (V=Voltage(Volt) I=Current(Ampere) R=Resistance(Ohm))</p>";
htmlcode=htmlcode+"<div id="abbr"><img src="abbr.jpg" height="200" width="150"/> </div>";
htmlcode=htmlcode+"<div id="img1" ><img id="loadingImage" src="graph.jpg" style="visibility:hidden"height="280" width="380"/></div>";
htmlcode=htmlcode+"<div id="circuit" ><img src="circuit.png" height="660" width="1130"/></div>";
htmlcode=htmlcode+"<div id="battery" ><img id="cells" src="reverse.png" style="visibility:hidden" height="180" width="270"/></div>";
htmlcode=htmlcode+"<table id="myTable">";
htmlcode=htmlcode+"<tr>";
htmlcode=htmlcode+"<th>Voltage</th>";
htmlcode=htmlcode+"<th>Current</th>";
htmlcode=htmlcode+"<th>Voltage/Current</th>";
htmlcode=htmlcode+"</tr>";
htmlcode=htmlcode+"</table>";
htmlcode=htmlcode+"<button  id="startQuiz" class="button" onclick="startQuiz()">Start Quiz</button>";
htmlcode=htmlcode+"<button  id="next" class="button" onclick="nextQuiz()">Next</button>";
htmlcode=htmlcode+"<button  id="submit" class="button" onclick="checkQuiz()">Submit answer</button>";
htmlcode=htmlcode+"<button  id="showQuiz" class="button" onclick="showQuiz()" >Show Answers</button>";
htmlcode=htmlcode+"<button  id="graph" class="button" onclick="showGraph()">Graph</button>";
htmlcode=htmlcode+"<button  id="note" class="button" onclick="note()">Note Values</button>";
htmlcode=htmlcode+"<button  id="reverse" onclick="reverseP()">Reverse Polarity</button>";
htmlcode=htmlcode+"<input type="checkbox" id="reverseCheck">"    
document.write(htmlcode);

This seems to be not working. 这似乎不起作用。 Please help. 请帮忙。

You have a problem because you don't escape double quotes : 您有一个问题,因为您无法避免使用双引号:

htmlcode = "<div class=\"transbox1\"> </div> <p id=\"heading\">OHMs LAW EXPERIMENT</p>";

You should consider using simple quotes for your string so you wouldn't have to escape the double quotes : 您应该考虑对字符串使用简单的引号,这样就不必转义双引号了:

htmlcode = '<div class="transbox1"></div><p id="heading">OHMs LAW EXPERIMENT</p>';

Other advice, you should use the "+=" notation 其他建议,您应该使用“ + =”表示法

htmlcode = '<div class="transbox1"></div>';
htmlcode += '<div class="transbox2"></div>';

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

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