Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将文本放入在外部javascript文件.js中创建的div中_Javascript - Fatal编程技术网

如何将文本放入在外部javascript文件.js中创建的div中

如何将文本放入在外部javascript文件.js中创建的div中,javascript,Javascript,我有个问题。如何将div放入外部javascipt文件.js中?什么是语法?不是将.js转换成已经在html页面中但在.js文件中的div!这个问题在这里被重新考虑和修改 我重复下面的代码在.js文件中 var div = document.createElement("div"); div.style.width = "100px"; div.style.height = "100px"; div.style.background = "red"; div.style.color = "whi

我有个问题。如何将div放入外部javascipt文件.js中?什么是语法?不是将.js转换成已经在html页面中但在.js文件中的div!这个问题在这里被重新考虑和修改

我重复下面的代码在.js文件中

var div = document.createElement("div");
div.style.width = "100px";
div.style.height = "100px";
div.style.background = "red";
div.style.color = "white";
我想做的是将下面的代码放到javascript文件.js中编写的div中!在与上面代码相同的.js文件中

  document.write("<h1>This is a heading</h1>");
  document.write("<p>This is a paragraph</p>"); 
对于大量字符串,可以将它们连接起来:

var str = '<h1>This is a heading</h1';
str += '<p>This is a paragraph</p>';
str += '<p>This is a another paragraph</p>';
...
div.innerHTML = str;

不,我需要将文本插入到同一个.js文件中的div中。是否有方法将大量document.write放入上面创建的div中,例如html中的一些文本?document.write是更新DOM的过时方法。您应该使用DOM操作方法。
var str = '<h1>This is a heading</h1';
str += '<p>This is a paragraph</p>';
str += '<p>This is a another paragraph</p>';
...
div.innerHTML = str;