表外的内容,不在其中?

时间:2011-03-26 13:49:26

标签: jquery html

为什么jQuery中的这个.html()函数会将post.username的超链接放在表格下面而不是在其中?

代码:

.html('<table width="138" height="24" border="0"><tr><td width="280" class="style3"><a href="../profile.php?user=' + post.username + '" class="post-title"></td></tr></table>' + post.username + '</a><p class="item-content">' + post.item_content + '<br /></p>')
.click(function() {
    window.location = postURL;
})

5 个答案:

答案 0 :(得分:1)

那是因为您的<a>标记未关闭。您有错误的元素嵌套:标记在<td>内打开并在外部关闭。此外,我会建议你以下:

.html(
    $('<table/>', {
        width: '138px',
        height: '24px',
        border: '0',
        html: $('<tr/>', {
            html: $('<td/>', {
                width: '280px',
                class: 'style3',
                html: $('<a/>', {
                    class: 'post-title',
                    href: '../profile.php?user=' + encodeURIComponent(post.username),
                    text: post.username
                })
            })
        })
    })
).append(
    $('<p/>', {
        class: 'item-content',
        value: post.item_content
    })
).click(function() {
    window.location = postURL;
});

答案 1 :(得分:1)

因为它在桌子之外。您的HTML已重新格式化:

<table width="138" height="24" border="0">
  <tr>
    <td width="280" class="style3">
      <a href="../profile.php?user=' + post.username + '" class="post-title">
    </td>
  </tr>
</table>
<!-- End of the table here -->
post.username</a>
<p class="item-content">
  post.item_content<br />
</p>

您可能希望将所有内容都放在<td>

答案 2 :(得分:0)

可能是因为你已经在表格之后关闭了一个标签。

答案 3 :(得分:0)

可能是因为您没有遵循正确的HTML格式?你的锚没有在桌子内关闭,它在外面关闭。您的插入内容如下:

<table width="138" height="24" border="0">
  <tr>
    <td width="280" class="style3">
      <a href="../profile.php?user=' + post.username + '" class="post-title">
    </td>
  </tr>
</table>
' + post.username + '</a> <!-- Shouldn't this be inside the table? -->
<!-- And this is fine below the table (presumably) if you want it there -->
<p class="item-content">' + post.item_content + '<br /></p>

答案 4 :(得分:0)

看起来你在关闭锚之前关闭了桌子

.html('<table width="138" height="24" border="0"><tr><td width="280" class="style3"><a href="../profile.php?user=' + post.username + '" class="post-title">' + post.username + '</a></td></tr></table><p class="item-content">' + post.item_content + '<br /></p>')