简体   繁体   English

使用javascript将HTML附加到正文的末尾

[英]Appending HTML to end of body using javascript

I have several templates for faceboxes (lightbox) that I need at different points of the application. 我有几个面板箱(灯箱)模板,我需要在应用程序的不同点。 These are stored in different partials and files. 它们存储在不同的部分和文件中。

I will initialize different javascript functions in accordance to which ones I need. 我将根据我需要的函数初始化不同的javascript函数。 The question is, what is the best way to append the external HTML page into my body using javascript? 问题是,使用javascript将外部HTML页面附加到我的身体的最佳方法是什么?

Since you tagged the question with it, here's a jQuery solution. 既然你用它标记了问题,这里是一个jQuery解决方案。

$("body").append("text");

Remember that the parameter can also be a DOM element. 请记住,该参数也可以是DOM元素。 So you can do this : 所以你可以这样做:

var p = $("<p/>").text("a paragraph");
$("body").append(p);

Also you can try some templates library.. Like handlebar and underscore.. 你也可以尝试一些模板库..像车把和下划线..

and append in the el provided by backbone.js 并附加在backbone.js提供的el中

the easy way with jQuery is: jQuery的简单方法是:

$('#result').load('test.html');

<div id="result"><!--/ Hold My Data! /--></div>

obviously you can change #result with body 显然你可以用身体改变#result

Suppose you want to append this html in your template, then you can use the below code according to your application Consider the code 假设您要在模板中附加此html,那么您可以根据您的应用程序使用以下代码考虑代码

Example 1: 例1:

rowData += '<div style="width: 130px;">'+param1+'</div>';
rowData += '<div style="width: 130px;">'+param2+'</div>';
$('#ID').html(rowData);

and please make sure that the js should be include in that file. 并请确保js应包含在该文件中。 Here is the information of variable used above: 以下是上面使用的变量的信息:

row data - the html that you want to append, 行数据 - 要追加的html,

param - if you want to show the value of java script variable on browser dynamically, param - 如果要在浏览器上动态显示java脚本变量的值,

#ID - ID of the div in which you want to append this html #ID - 要在其中附加此html的div的ID

example 2 : Consider the following HTML: 示例2 :考虑以下HTML:

<h2>Hello World</h2>
<div class="user">
  <div class="inner">Hi</div>
  <div class="inner">Bye</div>
</div>

You can create content and insert it into several elements at once: 您可以创建内容并将其一次插入多个元素中:

$( ".inner" ).append( "<p>GOD IS GREAT</p>" );

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

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