简体   繁体   English

在JavaScript中加载大量HTML

[英]Loading large amounts of html in javascript

How can I load large amounts of HTML in javascript? 如何在JavaScript中加载大量HTML? I will show a snippet below. 我将在下面显示一个片段。 I want to get all the HTML into the "ALL HTML GOES HERE" space in the java. 我想将所有HTML放入Java中的“ ALL HTML GOES HERE”空间中。 I know you can put smaller things like <h1>Hello!</h1> but I can't figure out how to get all of that into there with it working. 我知道您可以放一些较小的东西,例如<h1>Hello!</h1>但我不知道如何将所有内容放到那里。 Unless there is another way to do this let me know. 除非有另一种方法可以让我知道。

  function validate() { var x = $('#in').val(); if (navigator.userAgent.indexOf("Chrome") != -1) { $('#id').html('ignore'); } else { $('#id').html('ALL HTML GOES HERE'); } } window.onload = validate; 
 <div id="popup" class="overlay"> <div class="popup"> <a class="close" href="javascript:popupClose();">×</a> <div class="content"> </div> </div> </div> 

Instead of putting the HTML in a Javascript string, put it in the page's HTML, but hide it with display: none; 与其将HTML放入Javascript字符串中,不如将其放入页面的HTML中,而是通过display: none;将其隐藏display: none; style. 样式。 Then you can copy it to #id . 然后,您可以将其复制到#id

 function validate() { var x = $('#in').val(); if (navigator.userAgent.indexOf("Chrome") != -1) { $('#id').html('ignore'); } else { $('#id').html($("#allhtml").html()); } } window.onload = validate; 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="popup" class="overlay"> <div class="popup"> <a class="close" href="javascript:popupClose();">×</a> <div class="content"> </div> </div> </div> <div id="allhtml" style="display: none;"> <div> ALL HTML GOES HERE </div> </div> 

You can just pass it the entire string: 您可以将整个字符串传递给它:

function validate() {
  var x = $('#in').val();
  var htmlString = '<div id="popup" class="overlay">';
  htmlString += '<div class="popup">';
  htmlString += '<a class="close" href="javascript:popupClose();">×</a>';
  htmlString += '<div class="content"></div></div></div>';

if (navigator.userAgent.indexOf("Chrome") != -1) {
  $('#id').html('ignore');
} else {
  $('#id').html(htmlString);
}

if you don't want to write all html inside ' ' , you could load html with ajax, so first you would create file 如果您不想将所有html都写在''内,则可以使用ajax加载html,因此首先要创建文件

allhtml.html

containg your html, and there instead of your: 包含您的html,而不是您的:

 $('#id').html..

you would use 你会用

$('#id').load('allhtml.html')

One option that can help: 一种可以帮助您的选择:

You can put all the HTML in a separate HTML file and use JQuery's .load('anotherpage.html'....) method. 您可以将所有HTML放在单独的HTML文件中,并使用JQuery的.load('anotherpage.html'....)方法。 Basically, you'd just call that like: $('#id).load('....'); 基本上,您可以这样称呼:$('#id).load('....');

Here's the documentation with examples http://api.jquery.com/load/ 这是带有示例http://api.jquery.com/load/的文档

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

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