使用.load加载外部js文件,使社交按钮无法在刷新的div中工作

时间:2013-04-17 02:06:03

标签: jquery html

我有一个包含新闻容器,社交按钮容器和评论框容器的盒子。盒子有固定的高度所以我不能使用.append而不是我想刷新那个盒子。问题是当我在框中使用.load时社交按钮不起作用,因为它使用外部.js文件。这可能吗?

这是我的方框:

<div class="pin-box" id="pinbox_<?php echo $row['articles_id'];?>">
    <div class="box1">
        <?php
            //Script that will echo articles
        ?>


        <hr class="art_line">

        <div class="social_button" id="social_<?php echo $row['articles_id'];?>">

            <span class='st_facebook_large' st_url='<?php echo base_url(); ?>index.php/be_informed/show/<?php echo $row['articles_id']; ?>' displayText='Facebook'></span>
            <span class='st_twitter_large' st_url='<?php echo base_url(); ?>index.php/be_informed/show/<?php echo $row['articles_id']; ?>'  displayText='Tweet'></span>

            <span class='st_linkedin_large' st_url='<?php echo base_url(); ?>index.php/be_informed/show/<?php echo $row['articles_id']; ?>' displayText='LinkedIn'></span>
            <span class='st_googleplus_large' st_url='<?php echo base_url(); ?>index.php/be_informed/show/<?php echo $row['articles_id']; ?>' displayText='Google +'></span>
        </div>
        <div id="space"></div>

        <div id="beinformed_comment">
            <?php
                //Script that will echo comments
            ?>

        </div>
    </div>
</div>

<script type="text/javascript" src="http://w.sharethis.com/button/buttons.js"></script>
<script type="text/javascript">
        stLight.options({publisher: "751d0899-1764-4d1f-aac0-1bf112ad5cfd",
        doNotHash: false,
        doNotCopy: false, 
        hashAddressBar: false
        });
</script>

这是我的Jquery代码(我使用的是1.4.2):

$('.comment_button_article').live("click",function() 
{

var ID = $(this).attr("id");

var comment= $("#ctextarea"+ID).val();
var user_id= $("#user_id_"+ID).val();
var dataString = 'comment='+ comment + '&msg_id=' + ID + '&user_id=' + user_id;

if(comment=='' | comment=='Enter your comment here')
{
alert("Please Enter a Comment");
}
else

{
$.ajax({
type: "POST",
url: "comment_ajax.php",
data: dataString,
cache: false,
success: function(html){

$('#pinbox_'+ID).load('<?php echo base_url(); ?>index.php/be_informed/index #pinbox_'+ID);

 }
 });

}
return false;
});

1 个答案:

答案 0 :(得分:0)

我的猜测是它可能是外部脚本没有执行。

您应该尝试在将内容添加到页面后手动调用脚本(作为load()调用中的回调函数),例如:

$.ajax({
    type: "POST",
    url: "comment_ajax.php",
    data: dataString,
    cache: false,
    success: function(html){    
        $('#pinbox_'+ID).load('<?php echo base_url(); ?>index.php/be_informed/index #pinbox_'+ID, function(){
             $.ajax({
                dataType: "script",
                cache: true,
                url: 'http://w.sharethis.com/button/buttons.js'
            });
        });            
     }
});

}