在javascript中是否有php echo / print等效物 [英] Is there a php echo/print equivalent in javascript

查看:92
本文介绍了在javascript中是否有php echo / print等效物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我想从脚本标签内打印html。

Say I want to print html from inside a script tag.

像这样的来源

<div>foo</div>
<script>
print('<div>Print this after the script tag</div>');
</script>
<div>bar</div>

在脚本运行后应该在浏览器中看起来像这样

should look something like this in browser after the script has run

<div>foo</div>
<script>
print('<div>Print this after the script tag</div>');
</script>
<div>Print this after the script tag</div>
<div>bar</div>

我可以为此目的编写自己的代码,但因为这对我来说就像一个非常简单的问题,我猜想要么我错过了某些东西,要么我的想法在某种程度上是有缺陷的,并且有意地打印出来。

I could write my own code for this purpose but since this looks to me like a very simple problem, I'm guessing either I've missed something or my thinking is flawed in some way and printing is left out intentionally.

另外,有点相关:我想要知道脚本是否(或可以制作)知道它周围的脚本标签。有了这些信息,就可以更容易找到要注入的打印html代码的位置,假设它不是非常气馁。

Also, somewhat related: I'd like to know if a script is (or can be made) aware of the script tags surrounding it. With this information it would be much easier to find the position for the printed html code to be injected into, assuming it's not highly discouraged.

澄清:我不知道需要你为我写一个打印功能。我只需要知道是否存在实现此目的的本地方法,我错过了它或者不应该这样做的原因。

To clarify: I don't need you to write a print function for me. I only need to know if a native method to achieve this exists and I've missed it or alternatively the reason it shouldn't be done.

编辑
我意识到我没有想到这个问题。

EDIT I realized that I didn't think the question through.

我直截了当地知道了事实,现在几乎所有事情似乎都在起作用。我应该最初提到模板内部需要打印功能 - 我正在进行模板引擎实验。我设法通过从简单的html中分离脚本并将分离的html sans脚本与脚本输出连接来解决这个问题。

I got my facts straight and now almost everything seems to be working. I should've originally mentioned that the print function was needed inside templates - I'm working on a template engine experiment. I managed to work it out by separating scripts from plain html and concatenating the split html sans scripts with script output.

当我编写代码时,我注意到一切都不会'因为js的异步性质,所以顺利进行。我想我希望能够在模板中做任何类型的js魔术,就像我在php中一样。似乎实际上在模板内部以傻瓜式方式支持异步代码需要更多考虑。

As I was writing the code I noticed that everything wouldn't go so smooth because of the asynchronous nature of js. I guess I was expecting to be able to do any kind of js magic in templates, just like I could in php. Seems like actually supporting async code in a fool-proof manner inside templates will require some more thought.

推荐答案

你需要使用 document.write()

<div>foo</div>
<script>
document.write('<div>Print this after the script tag</div>');
</script>
<div>bar</div>

请注意,这仅在您编写文档的过程中有效。文档渲染完成后,调用 document.write()将清除文档并开始编写新文档。如果这是您的使用案例,请参阅此问题提供的其他答案。

Note that this will only work if you are in the process of writing the document. Once the document has been rendered, calling document.write() will clear the document and start writing a new one. Please refer to other answers provided to this question if this is your use case.

这篇关于在javascript中是否有php echo / print等效物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆