javascript - 如何使用 Javascript 输出带有变量插值的多行 HTML?

标签 javascript jquery variables interpolation

我试图在每次运行函数时输出以下内容:

<object id="video" width="500" height="500" type="application/x-shockwave-flash" data="http://releases.flowplayer.org/swf/flowplayer-3.2.1.swf">
<param name="movie" value="http://releases.flowplayer.org/swf/flowplayer-3.2.1.swf" />
<param name="allowfullscreen" value="true" />
<param name="flashvars" value='config={"playlist":["poster_location", {"url": "clip_location","autoPlay":false,"autoBuffering":true}]}' />
<img src="poster_location" width="500" height="500" alt="Poster Image" title="No video playback capabilities." />
</object>

我想在代码的某些部分插入 javascript 变量(以更改视频/海报等)。

我意识到这可以通过使用 document.getElementById 定位需要更改的每个部分并以这种方式进行更改来完成,但是由于各种原因,我需要删除并重新构建每次运行函数时的整个元素。

那么有没有一种类似php的echo的方式使用javascript输出html的好方法呢?

如果有帮助的话,我也在使用 jQuery。

最佳答案

您可以像这样在 JavaScript 中使用多行字符串:

var str = "Blah blah blah \
    note the backslash that escapes the end of line";

或者,您可以简单地关闭字符串并在下一行重新打开它:

var str = "Blah blah blah "
    +"and this way you don't get 'Empty Text Node' all over your DOM";

关于javascript - 如何使用 Javascript 输出带有变量插值的多行 HTML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4328132/

相关文章:

c++ - 为什么我不能用普通变量做多态?

variables - 获取变量的第一个字符

python - 变量名称给出语法错误(Python)

javascript - 然后没有触发 Jasmine 的提取模拟

javascript - 在 Rails 应用程序中访问 Javascript 变量

JavaScript 原型(prototype)属性 : Prototype based Inheritance

javascript - 如何将表示时间的字符串添加到只有日期的时刻对象

javascript - 访问自执行函数

javascript - 异步和等待方法

javascript - 添加监听器时 Google Maps V3 map 未加载