doushi1996 2012-04-07 09:18
浏览 28

jquery库不能在外部php文件中工作

I have any page and included jquery library and css to this (index).

Now I have code for twitter like load more. This worked but when I need to delete this using jQuery it is not working ( with another code in my index ). problem this : jquery library is not appended to the external php file.

I have same problem in colorbox. when I load iframe in colorbox my index jQuery not appended to external files so I put manual jquery to external files!

There is no way that only used the jquery original file(my index) ?

E.X js load more :

<script type="text/javascript">
$(function() {
//More Button
$('.more').live("click",function() 
{
var ID = $(this).attr("id");
if(ID)
{
$("#more"+ID).html('<img src="moreajax.gif" />');

$.ajax({
type: "POST",
url: "ajax_more.php",
data: "lastmsg="+ ID, 
cache: false,
success: function(html){
$("ol#updates").append(html);
$("#more"+ID).remove();
}
});
}
else
{
$(".morebox").html('The End');

}


return false;


});
});

</script>

PHP :

 <?php
include("config.php");


if(isSet($_POST['lastmsg']))
{
$lastmsg=$_POST['lastmsg'];
$result=mysql_query("select * from messages where msg_id<'$lastmsg' order by msg_id desc limit 9");
$count=mysql_num_rows($result);
while($row=mysql_fetch_array($result))
{
$msg_id=$row['ms_gid'];
$message=$row['message'];
?>


<li>
<?php echo $message; ?>
</li>


<?php
}


?>

<div id="more<?php echo $msg_id; ?>" class="morebox">
<a href="#" id="<?php echo $msg_id; ?>" class="more">more</a>
</div>

<?php
}
?>

HTML :

    <head><script type="text/javascript" src="./jquery.min.js"></script></head>
    <div id='container'>
    <ol class="timeline" id="updates">
    <?php
    $sql=mysql_query("select * from messages ORDER BY msg_id DESC LIMIT 9");
    while($row=mysql_fetch_array($sql))
    {
    $msg_id=$row['msg_id'];
    $message=$row['message'];
    ?>
    <li>
    <?php echo $message; ?>
    </li>
    <?php } ?>
    </ol>
    <div id="more<?php echo $msg_id; ?>" class="morebox">
    <a href="#" class="more" id="<?php echo $msg_id; ?>">more</a>
    </div>
    </div>
  • 写回答

1条回答 默认 最新

  • duancheng7743 2012-04-07 09:30
    关注

    Is is because your jquery is loaded after the document is ready then you call the other php file. You should use jquery functions without $(document).ready(); so you will be able to use jquery functions everywhere. The other important thing is that instead of using

    $(function(){
        $('#selector').click(function(){
           'your other code'  
    
        });
    });
    

    use like this

    function my_function(){
      your code
    }
    

    and html

    <input type = "text" onclick = 'my_function();'>
    

    Hope this will help. This way you will be able to call these functions everywhere. And if you are calling or loading the other php file in the first php then make sure it is loaded in a div which resides in the first php file.

    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b