简体   繁体   English

如何在不呈现HTML的情况下将dom元素放置在HTML中?

[英]How to place dom element in HTML without rendering it?

I need to place html/javascript code in html page that I don't want to render/run at page load. 我需要将HTML / javascript代码放在不想在页面加载时呈现/运行的html页面中。

I want to get this content later with `innerHTML' and use it dynamical. 我想稍后通过“ innerHTML”获取此内容并动态使用它。

How can code be saved in html page so that browsers ignore it but it can be manipulated with Javascript dom interaction. 如何将代码保存在html页面中,以便浏览器将其忽略,但可以通过Javascript dom交互对其进行操作。

 <script norun="true" type="text/xml">
          <script type="text/javascript"></script>
          <div>html</div>
 </script>

Above code is not working, because closing tag of inner <script> tag closes the parent "text/xml" type script. 上面的代码不起作用,因为内部<script>标记的close标记关闭了父“ text / xml”类型的脚本。 Need corss-platform method to keep JS/HTML inside dormant and intact. 需要使用corss-platform方法来使JS / HTML保持休眠和完整。

Future solution: HTML5 <template> . 未来的解决方案:HTML5 <template> But it's not supported on IE 但是IE不支持

Safe solution: entity-escape or base64 encode and decode in javascript 安全的解决方案:使用Javascript进行实体转义或base64编码和解码

Hacky: <svg style="display:none;"><![CDATA[ your inert content here ]]></svg> , since html5 itself does not allow cdata but an exception is made for foreign content, ie svg and mathml hacky: <svg style="display:none;"><![CDATA[ your inert content here ]]></svg> ,因为html5本身不允许cdata,但对外部内容(例如svg和mathml)进行了例外处理

When I do a similar thing (usually with view fragments for SPA-style pages) I generally go with the script tag approach similar to what you were doing. 当我做类似的事情时(通常使用SPA样式页面的视图片段),我通常会采用与您正在执行的脚本标签类似的方法。 This is assuming I'm using Hogan.js but you can ignore those details for the example: 假设我使用的是Hogan.js,但是您可以忽略该示例的详细信息:

<script type="text/hogan-template" id="tweet-template">
  <div class="tweet">
    <div class="username">{{username}}</div>
    <div class="body">{{body}}</div>
    <div class="date-posted">{{posted}}</div>
  </div>
</script>

From here you can fetch it via JavaScript when you need it and the browser ignores it as HTML. 在需要时,您可以从此处通过JavaScript获取它,而浏览器会将其忽略为HTML。

var tweetTemplateEl = document.querySelector("#tweet-template"),
    tweetTemplate = hogan.compile(tweetTemplateEl.innerHTML);

var tweetList = document.querySelector("#tweet-list"),
    tweetItem = document.createElement("li");

tweetItem.innerHTML = tweetTemplate.render(tweetData);
tweetList.appendChild(tweetItem);

For JavaScript, you could use something like a placeholder on the page. 对于JavaScript,您可以在页面上使用类似占位符的方式。 I'm not really sure the most semantic tag to use so I'll use div as it's generally a go to "default" tag. 我不太确定要使用的语义标签最多,因此我将使用div因为它通常是转到“默认”标签的地方。 So, in place of where you'd place an include you could simply do: 因此,可以在包含位置的地方简单地执行以下操作:

<div class="script-placeholder" id="some_js_file_name" data-href="/js/some_js_file_name.js"></div>

This could be used in multiple ways but here's one option: 这可以有多种使用方式,但这是一个选择:

window.require = function(fileName) {
  var body = document.querySelector("body"),
      placeholder = document.querySelector(["#", fileName].join("")),
      src = placeholder.dataset.href,
      script = document.createElement("script");
  script.src = src;
  body.appendChild(script);
};

Which you can then add to whatever you'd like to initiate a require: 然后,您可以将其添加到要启动需求的任何内容中:

someElement.addEventListener("click", function(e) {
  require("some_js_file_name");
});

That might be something that allows you to achieve what you'd like to achieve. 那可能是使您实现想要达到的目标的东西。

NOTE: It's important to note that if you really want to go the dynamic load route you should probably invest in an established library to accomplish this like CommonJS if you'd like the style of requires like I've shown here. 注意:需要特别注意的是,如果您确实想采用动态加载路线,那么您可能需要投资已建立的库来实现此目标,例如CommonJS,如果您希望这种需求的样式如我在此显示的那样。 This handwritten sample is just that, it's a sample to give you an idea of what is going on. 这个手写的示例仅是一个示例,它使您可以了解发生了什么。

Wrap the code in an HTML comment: <!-- ... --> 将代码包装在HTML注释中: <!-- ... -->

Browsers will not parse or render anything inside of the comment. 浏览器不会解析或呈现注释中的任何内容。

When you want to initiate rendering, uncomment the code. 要启动渲染时,请取消注释代码。 This can be done by wrapping the comment in an identifiable element, getting its innerHTML, stripping out the comment syntax, and resetting the innerHTML of the element to be the live uncommented markup. 这可以通过以下方式完成:将注释包装在可识别的元素中,获取其innerHTML,剥离注释语法,然后将元素的innerHTML重置为实时的未注释标记。

http://jsfiddle.net/92du461p/2/ http://jsfiddle.net/92du461p/2/

<button id="b">Show hidden content</button>
<div id="delay-render"><!-- <p>hide me</p> --></div>
<script>
  var r = document.getElementById('delay-render');
  var b = document.getElementById('b');
  b.addEventListener('click', function() {
    r.innerHTML = r.innerHTML.match(/^<!--(.*)-->$/)[1]
  });
</script>

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

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