2012-04-07 83 views
-3

我有任何页面,并包括jQuery库和CSS(this)。jquery库不能在外部php文件中工作

现在我有代码为Twitter像加载更多。这工作,但当我需要删除这使用jQuery它不起作用(与我的索引中的另一个代码)。问题是:jQuery库不会附加到外部的php文件。

我在colorbox中有同样的问题。当我在colorbox中加载iframe时,我的索引jQuery不会附加到外部文件,所以我将手动jQuery添加到外部文件!

有没有办法只使用jquery原始文件(我的索引)?

EX JS负载更多:

<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> 
+2

你的问题缺乏清晰度。到目前为止,它似乎意味着你可以成功地在一个php文件中加载和使用jquery,但它不会自动提供给其他php文件。那是对的吗?没有更具体的例子和信息,向你提供有用的答案将是非常困难的。 – Vijay 2012-04-07 09:24:41

+0

请参阅我的更新信息。这完全e.x – Gcoder 2012-04-07 09:35:57

回答

0

是因为您的jQuery是加载后的文件准备好就拨打其他的PHP文件。你应该使用没有$(document).ready()的jquery函数;所以你将能够在任何地方使用jQuery的功能。另一个重要的事情是,而不是使用

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

    }); 
}); 

使用这样

function my_function(){ 
    your code 
} 

和HTML

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

希望这会有所帮助。这样你就可以在任何地方调用这些函数。如果你在第一个php中调用或者加载另一个php文件,那么确保它被加载到一个驻留在第一个php文件中的div中。

+0

如何使用我的e.x? – Gcoder 2012-04-07 12:13:31