2011-09-05 131 views
0

我有一个用Smarty编译的PHP模板。在这段代码中我已经嵌入DIV部分,与此类似:JQuery追加不工作?

<div> 
<div id="services" style="margin-top: 5px; width: 100%; display:none;"> 
<div id='blocking' style="float: left; width: 100%;"> 
    <div id="div1" ...> 
    </div> 

    <div id="div2" style="float: left; height:100px; width: 100%; margin-top: 4px; margin-bottom:4px;"> 
    <table id="list" width="100%"> 
    <thead> 
    <tr> 
     <th width="30%" class="tablaIzq">Lists</th> 
     <th width="70%" class="tablaIzq">Description</th> 
    </tr> 
    </thead> 
    <tbody id="LBBody">     
    </tbody> 
    </table> 
    </div> 
</div> 
</div> 
</div> 

使用AJAX + JSON称为一个WebService ES ...我希望把一些数据,我收到的TBODY LBBody但是当我尝试使用jquery的追加没有附加。

它的完成这样的:

$("#LBBody").append('<tr><td class="tablaIzq" style="font-weight: normal; height: 22;" colspan="3">Hi</td></tr>') 

我使用追加错?

感谢和问候。

+0

您是否在DOM就绪状态下调用append()? 即在$(function(){[...]});'? 否则当你调用append() – HBublitz

回答

1

确保你在你的实际元素后面加上你的追加代码。否则它不会工作

2

尝试,包括在$(document).ready()块这样

$(document).ready(function() { 
    $("#LBBody").append('<tr><td class="tablaIzq" style="font-weight: normal; height: 22;" colspan="3">Hi</td></tr>'); 
}); 
+0

追加发生在ajax调用 – numbers1311407

+0

@marcioAlmada是的,我认为我们需要看到'$ .ajax()函数可能不存在你想追加节点的元素可能不存在。 '块。 –

0

你tryed检查你的DOM,看是否元素被追加或不追加你的代码?我有一种感觉,参数中的height属性可能会将您的新元素设置为0高度显示。

为了解决这个问题,你应该通知相应的值加上统一:

style="height:22px;"

-1

PHP和JavaScript(或JQuery的)不一起工作。如果您尝试将它们一起使用,整个Javascript将停止运行。因此,避免这种情况的最好方法是使用JQuery AFTER使用PHP。完成PHP代码后,使用“?>”标记结束,然后使用JQuery,而不使用PHP的echo或print语句。

这是错误的代码和JavaScript将无法在这方面的工作:

<?php 
echo '<script type="javascript/text">...</script>'; 
?> 

这是正确的实现:

<?php 
echo '...'; 
?> 
<script type="javasrcipt/text">...</script> 

希望这有助于!