Javascript 内联输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6566318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript inline output
提问by dclowd9901
In javascript suppose you have this piece of code:
在javascript中假设你有这段代码:
<div>
<script type="text/javascript">var output = 'abcdefg';</script>
</div>
Is there any way to simply "echo" (using PHP terminology) the contents of output
into #test
? That is, to output a string inline, assuming you have no standard way of traversing or selecting from the DOM?
有什么方法可以简单地“回显”(使用 PHP 术语)output
into的内容#test
?也就是说,要输出一个内联字符串,假设您没有从 DOM 中遍历或选择的标准方法?
document.write("...");
does write the contents of output
, but in doing so, it replaces the entire document with the output
.
document.write("...");
确实写入 的内容output
,但这样做时,它将整个文档替换为output
.
The solution I'm looking for should act the same way a PHP echo
would: write the output into the document inline.
我正在寻找的解决方案应该以与 PHP 相同的方式行事echo
:将输出内联写入文档中。
采纳答案by jerluc
"Kosher" code aside, yes.
撇开“Kosher”代码不谈,是的。
document.write()
document.write()
回答by Felix Kling
You'd have to use document.write
[docs]:
你必须使用document.write
[docs]:
<div id="test">
<script type="text/javascript">document.write('abcdefg');</script>
</div>
With the caveat that it does not work in XHTML documents. See the documentation for more details.
需要注意的是它在 XHTML 文档中不起作用。有关更多详细信息,请参阅文档。
回答by Matthew Flaschen
回答by Julien Ducro
You could write something like:
你可以这样写:
<div id="test">
<script>
document.getElementById('test').innerHTML = "stuff";
//this line only changes content in the div with id="test", not the whole dom
</script>
</div>
But you should avoid putting a script inside a div because it may be overwritten.
但是您应该避免将脚本放在 div 中,因为它可能会被覆盖。
回答by jfriend00
If your code is in the div, then document.write('abcdefg')
is the proper choice for inserting something inline at the point of execution.
如果您的代码在 div 中,那么document.write('abcdefg')
在执行点插入内联内容是正确的选择。
Or, if your code is not inside the div, you can do this:
或者,如果您的代码不在 div 内,您可以这样做:
<div id="test">
</div>
<script type="text/javascript">
var output = 'abcdefg';
document.getElementById("test").innerHTML = output;
</script>
You will have to make sure that your code runs AFTER the page is loaded and the div is present.
您必须确保您的代码在页面加载并且 div 存在后运行。
回答by Relaxing In Cyprus
I know this is an old question but if anybody is still looking then here is a handy function that does the job.
我知道这是一个老问题,但如果有人仍在寻找,那么这里有一个方便的功能可以完成这项工作。
function echo(text)
{
document.body.appendChild(document.createElement("div")).appendChild(document.createTextNode(text));
}
回答by Kieran
console.log()
控制台日志()
also supported in chrome and ie 9.. watch out for backwards compatibly it will get you ie8 and down i think...
也支持 chrome 和 ie 9 .. 注意向后兼容它会让你 ie8 和向下我认为......