使用 jquery .html() 插入 html

发布于 2024-09-14 18:19:07 字数 199 浏览 7 评论 0原文

我想将一大块 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

靑春怀旧 2024-09-21 18:19:08

您可能应该使用 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.

十二 2024-09-21 18:19:08

您还可以使用重音符号 (`),如下所示:

$("#menu").html(`
    
Options:
<a href="main.html">Home</a> | 
<a href="list.html">List</a> |
Add |
Options |
<a href=# id="logout">Logout</a>
         
`);

You can also use the grave accent ( ` ), like that:

$("#menu").html(`
    
Options:
<a href="main.html">Home</a> | 
<a href="list.html">List</a> |
Add |
Options |
<a href=# id="logout">Logout</a>
         
`);
雪化雨蝶 2024-09-21 18:19:07

我建议将 html 统一为一个字符串......就像这样。

htmlStr = "";
htmlStr += "<p>some paragraph";
htmlStr += "<p>another paragaraph</p>";

$("#content").html(htmlStr);

这样你就可以看到你的 html 哪里出了问题,并且它为 javascript 创建的内容增加了很多可读性。

另外...

如果您想保留此 TD 中的内容,我将使用 append() jquery 方法。


jquery 文档 是您最好的朋友!

I would suggest unifying the html into one string... like so.

htmlStr = "";
htmlStr += "<p>some paragraph";
htmlStr += "<p>another paragaraph</p>";

$("#content").html(htmlStr);

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!

扛起拖把扫天下 2024-09-21 18:19:07

Javascript 对多行字符串或 HEREDOC 语法没有很好的支持,但有一些解决方法。

在每行末尾添加一个反斜杠“\”,让脚本引擎知道您正在继续下一行而不完成:

<script>
var my_html = '\
    <div id="my-div">\
        <span>Name:</span> Your Name\
    </div>\
';
</script>

使用 XML CDATA hack(http://mook.wordpress.com/2005/10/30/multi-line-strings-in-javascript/):

<script>
var my_html = (<r><![CDATA[

    <div id="my-div">
        <span>Name:</span>Your Name
    </div>

]]></r>).toString();
</script>

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:

<script>
var my_html = '\
    <div id="my-div">\
        <span>Name:</span> Your Name\
    </div>\
';
</script>

Use an XML CDATA hack(http://mook.wordpress.com/2005/10/30/multi-line-strings-in-javascript/):

<script>
var my_html = (<r><![CDATA[

    <div id="my-div">
        <span>Name:</span>Your Name
    </div>

]]></r>).toString();
</script>
橘味果▽酱 2024-09-21 18:19:07

我要做的是将所有 HTML 放入 div 中,如下所示:

$("td#content").html($("#myHTML").html());

问题可能是换行符。如果你有一个字符串(用“”或“”括起来),那么你会发现换行符会“破坏”你的代码......所以你必须将所有 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:

$("td#content").html($("#myHTML").html());

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.

送君千里 2024-09-21 18:19:07

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 be html("<a href='#' onclick='dofunction(\"text\")'>...etc..."); in order for the double quotes to not be read literally.

杀お生予夺 2024-09-21 18:19:07

如果引号破坏了您的代码,请尝试以下操作...

$("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. ;)

青春如此纠结 2024-09-21 18:19:07

如果它来自链接,您可能需要使用 AJAX 来完成:

$("#YOUR_ELEMENT").load("THE_CONTENT_TO_LOAD");

但是,请记住,如果您尝试从不同域上的站点提取 HTML,则需要使用服务器代理等东西......有点超出了问题的范围。

If it's coming in from a link, you'll probably want to do it with AJAX:

$("#YOUR_ELEMENT").load("THE_CONTENT_TO_LOAD");

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文