2012-04-07 85 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