繁体   English   中英

当结果为JSON时如何在JQuery中编写HTML代码

[英]How to write HTML codes inside JQuery when results getting JSON

我有一些通过Json JQuery显示的HTML内容。

该内容几乎有400行。 当我在Jquery中编写代码时,我必须删除所有空白。 我的意思是这样

var code = "<div><a href="">this is test</a><p>This is paragraph</p></div>";

这样的事情。 上面的代码不是问题。 这是一个简单的代码。但是当我想使用更多代码时,这并不容易。 所以有什么简单的方法可以在Jquery中编写HTML代码?

您可以使用`字符代替“。

编辑: Internet Explorer当前不支持此功能。 相反,您可以在行尾使用\\:

 var text = "<h1>big</h1> \\ <h2>smaller</h2> \\ <h3>even smaller</h3> \\ <h6>smallest</h6>"; document.write(text); 

 var text = `<h1>big</h1> <h2>smaller</h2> <h3>even smaller</h3> <h6>smallest</h6>`; document.write(text); 

我只会猜你在问什么

 //supposed you have like this var precode = '<div>\\n\\r<a href="">this is test</a>\\n\\r<p>This is paragraph</p>\\n\\r</div>'; document.body.innerHTML = precode; //and you want to remove spaces like this var code = precode.replace(/\\s{2,}/g, ""); document.body.innerHTML += code; console.log("precode:", precode); console.log("code:", code); 

为了使内容更整洁,您可以将HTML保留在单独的文件中,但可以使用jQuery的.load()方法加载它:

$( "<div />" ).load( "/your_file.html", function() {
  // TODO: use this.innerHTML to get HTML string, or $(this) as jQuery element
});

暂无
暂无

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

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