简体   繁体   English

将已声明的html内容嵌入页面中的多个位置

[英]Embed once declared html content at muliple places within a page

I wish to use/embed html content (in this case a list <ul> ) at many places, but declare it only once with in a page. 我希望在许多地方使用/嵌入html内容(在本例中为列表<ul> ),但在页面中仅声明一次。

For example, consider the content: 例如,考虑以下内容:

<ul>
  <li>A</li>
  <li>B</li>
  <li>...</li>
  <li>X</li>
</ul>

Now this content needs to be used at many places like the following: 现在,此内容需要在许多地方使用,如下所示:

Use 1: 使用1:

<div class='left'>
  <ul id='listA'>
    <li>A</li>
    <li>B</li>
    <li>...</li>
    <li>X</li>
  </ul>
</div

Use 2: 使用2:

<div class='right'>
  <ul id="listX">
    <li>A</li>
    <li>B</li>
    <li>...</li>
    <li>X</li>
  </ul>
</div>

I am sure this can be done in server side or through Ajax calls in client side, but the objective is to optimize/reduce the html content (bytes of data) send across for every page request. 我确信这可以在服务器端或通过客户端的Ajax调用来完成,但是目的是优化/减少每个页面请求发送的html内容(数据字节)。 So, if that particular content appears in n places in a page, it must be sent only once , then manipulated there to all n distinct places. 因此,如果特定内容出现在页面的n位置中,则必须仅发送一次 ,然后在其中将其操作到所有n不同的位置。 Thus doing it in server side doesn't makes sense and Ajax will add the transportation overload, since it is a separate call. 因此,在服务器端这样做是没有意义的,Ajax将增加传输过载,因为这是一个单独的调用。

I assume there is nothing in html/css to do so, even iframe cannot be used for this purpose. 我认为html / css中没有任何可做的事情,即使iframe也不能用于此目的。 However I am sure, javascript/jQuery can do so. 但是我敢肯定,javascript / jQuery可以做到这一点。 How? 怎么样?

  1. I really suggest that you have a look at the Handlebars(js framework.) Shortly speaking, it's a js template framework. 我真的建议您看一下Handlebars(js框架。)简而言之,这是一个js模板框架。 http://handlebarsjs.com/ http://handlebarsjs.com/

  2. In this case, I suggestion you use requirejs to define your module with the html fragment and then whenever you need to use it you can require it and compile the template into what you want. 在这种情况下,我建议您使用requirejs用html片段定义模块,然后每当需要使用它时,都可以要求它并将模板编译为所需的模板。

  3. Also you can use jquery or other js lib to change the attribute of the html element as Philip Walton said. 您也可以使用jQuery或其他js lib来更改html元素的属性,如Philip Walton所说。 $("ul").eq(0).removeClass("right").addClass("left") $( “UL”)。EQ(0).removeClass( “右”)。addClass( “左”)

The best option I can see is use javascript. 我能看到的最好的选择是使用javascript。 You can use JQuery for easy syntax. 您可以使用JQuery简化语法。 Just define this reusable markup in one variable and insert it at various places on document ready event by setting innerHtml of your left and right divs. 就在一个变量定义这个可重复使用的标记,并在对文档准备事件不同的地方通过设置的innerHTML的插入leftright的div。

$(document).ready(function() {
   var reusablehtml ="<ul id='nameplaceholder'><li>A</li><li>B</li><li>...</li><li>X</li></ul>";
   $("#left").html(reusablehtml.replace("nameplaceholder", "listA"));
   $("#right").html(reusablehtml.replace("nameplaceholder", "listX"));
});

If the HTML is already on the page somewhere, you can just copy it in jQuery, change what you need to change, and then insert it somewhere else. 如果HTML已在页面上的某个位置,则可以将其复制到jQuery中,更改需要更改的内容,然后将其插入其他位置。

For example, if the only thing on your page is the <ul> you could do this: 例如,如果页面上唯一的内容是<ul> ,则可以执行以下操作:

var originalUL = $("ul");

$("body").append($('<div class="left" />').append(originalUL.clone().attr('id', 'listA')));

$("body").append($('<div class="right" />').append(originalUL.clone().attr('id', 'listB')));

Here's an example: http://jsfiddle.net/uU7cV/ 这是一个示例: http : //jsfiddle.net/uU7cV/

However, if you're going to be doing this a lot, and the changes to each instance are complex, I'd look into using a Javascript templating system or an Javascript MVC framework like Backbone.js http://documentcloud.github.com/backbone/ 但是,如果您要执行很多操作,并且每个实例的更改都很复杂,那么我会考虑使用Javascript模板系统或Javascript MVC框架(例如Backbone.js http://documentcloud.github)。 COM /骨干网/

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

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