【问题标题】:XHTML possibly blocking jQuery method .append?XHTML 可能会阻止 jQuery 方法 .append?
【发布时间】:2014-01-22 10:38:20
【问题描述】:

我正在尝试改进我的网站,添加将在每个页面中都有的部分,并在我更新文件时自动更新:我知道有很多方法,但我仍然是初学者,我决定使用 jQuery。问题是我使用了<table>,因为我有一个栏 div,它需要位于 HTML 中的内容旁边。我在表头、表格的开始标签(包括 tr 和 td)及其内容之前加上了标签的结束。 HTML 包含栏附近的任何内容:确实如此!但它在栏下:我不太确定发生了什么:我的想法是 XHTML 在附加之前自动关闭标签,因为在 HTML 中它没有找到它们......我将包括我们的代码有兴趣

$(document).ready(function(){
    $('body').prepend('<table><tr><td>Text</td><td>');
    $('body').append('</td></tr></table>');
})

那是 jQuery 代码,我可以确保你有正确的链接到脚本和 jQuery (+UI) 的工作。现在一切都应该很简单:您只需在 .html 文件中添加您的 HTML 代码...但是不!

<html>
   <head>
      <title>Cheese</title>
      <script to my script and jQuery library/>
   </head>
   <body>
      <!-- here should go what I prepended, and it does,
           but closes the <table></td></tr> tags aswell -->
      This should be to the side of 'Text' and comprehended in the table, in a td
      <!-- here should go what I appended,
           but strangely it doesn't -->
   </body>
</html>

我做错了吗?如果您在这里没有发现任何问题,我会将所有代码发送给您,提前谢谢!

【问题讨论】:

    标签: jquery html xhtml append prepend


    【解决方案1】:

    您不能将标签附加/前置/插入到 DOM 中,仅完成 html 元素。当您传递要插入 DOM 的部分 html 片段时,浏览器(或者可能是 jquery)会尽其所能。
    你应该做的是用描述的内容构建一个表并将其插入到dom中

    $(document).ready(function(){
        $('body').html('<table><tr><td>Text</td><td>'+$('body').html()+'</td></tr></table>');
    })
    

    http://jsfiddle.net/yY92d/

    【讨论】:

      猜你喜欢
      • 2021-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-20
      • 2016-06-09
      • 1970-01-01
      • 2010-10-24
      • 1970-01-01
      相关资源
      最近更新 更多