使用 jquery .html() 插入 html
我想将一大块 html 插入到预先存在的 中。我正在使用这个方法:
$("td#content").html(LOTS_OF_HTML_CODE_HERE);
但是它不起作用。我是一个菜鸟,HTML 块中有很多引号和 '
似乎破坏了它。这样做的正确方法是什么?
I want to insert a large chunk of html into a pre-existing <td>
. I am using this method:
$("td#content").html(LOTS_OF_HTML_CODE_HERE);
But it doesn't work. I am a noob, there are a lot of quotes and '
within the HTML chunk that seems to be breaking it. What is the correct method for doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可能应该使用 jquery 创建元素,而不是使用大字符串。如果你只使用一个大字符串,你就无法确定 dom 的正确性,并且几乎不可能调试或修改。
这类似于手动构建 xml。这只是不是首选的方式。
You should probably be creating your elements with jquery, rather than in a big string. You get no certainty of dom correctness if you just use a big string, and it's near impossible to debug or modify.
This is similar to building xml by hand. It's just not the preferred way of doing it.
您还可以使用重音符号 (`),如下所示:
You can also use the grave accent ( ` ), like that:
我建议将 html 统一为一个字符串......就像这样。
这样你就可以看到你的 html 哪里出了问题,并且它为 javascript 创建的内容增加了很多可读性。
另外...
如果您想保留此 TD 中的内容,我将使用
append()
jquery 方法。jquery 文档 是您最好的朋友!
I would suggest unifying the html into one string... like so.
this way you can see where your html is breaking down and it adds a lot of readability to javascript created content.
Also...
if there is content in this TD that you'd like to keep, I would use the
append()
jquery method.the jquery documentation is your best friend!
Javascript 对多行字符串或 HEREDOC 语法没有很好的支持,但有一些解决方法。
在每行末尾添加一个反斜杠“\”,让脚本引擎知道您正在继续下一行而不完成:
使用 XML CDATA hack(http://mook.wordpress.com/2005/10/30/multi-line-strings-in-javascript/):
Javascript doesn't have good support for multi-line strings or HEREDOC syntax, but there are a few workarounds.
Add a backslash "\" to the end of each line to let the script engine know you are continuing onto the next line without finishing:
Use an XML CDATA hack(http://mook.wordpress.com/2005/10/30/multi-line-strings-in-javascript/):
我要做的是将所有 HTML 放入 div 中,如下所示:
LOTS OF HTML HERE
那么什么时候执行此操作:问题可能是换行符。如果你有一个字符串(用“”或“”括起来),那么你会发现换行符会“破坏”你的代码......所以你必须将所有 HTML 放在一行上。
What I would do is to put all of that HTML into a div as per:
<div id="myHTML" style="display: none;">LOTS OF HTML HERE</div>
then when do this:What could be the problem is line breaks. If you have a string (enclosed in "" or '') then you will find that line breaks will "break" your code... so you'll have to put all the HTML on one line.
Stefan 是对的,但是要用引号回答你的问题,不要忘记转义任何可能会破坏它的内容,所以如果你有
html("...等等...");
它需要是html("
;...etc...");
以便双引号不按字面意思读取。Stefan is right, but to answer your question with quotes, don't forget to escape anything that would otherwise break it, so if you have
html("<a href='#' onclick='dofunction("text")'>...etc...");
it needs to behtml("<a href='#' onclick='dofunction(\"text\")'>...etc...");
in order for the double quotes to not be read literally.如果引号破坏了您的代码,请尝试以下操作...
$("td#content").html("我的链接< /a>");
引号没有被转义。只需在 HTML 代码中的任何双引号或单引号前添加反斜杠即可。应该可以做到这一点。 ;)
If quotes are breaking your code, try this...
$("td#content").html("<a href=\"http://www.example.com\">my link</a>");
The quotes are not being escaped. Just add a backslash before any quotes, double or single within your HTML code. That should do it. ;)
如果它来自链接,您可能需要使用 AJAX 来完成:
但是,请记住,如果您尝试从不同域上的站点提取 HTML,则需要使用服务器代理等东西......有点超出了问题的范围。
If it's coming in from a link, you'll probably want to do it with AJAX:
However, keep in mind if you're trying to pull HTML from a site on a different domain you'll need to use server proxies and all that stuff...kind of out of the scope of the question.