问题描述
标题:
< script type =text / javascriptsrc =http://code.jquery。 COM / jquery的-1.7.2.min.js>< /脚本>
< script type =text / javascriptsrc =js / timeago.js>< / script>
< script src =js / test_helpers.jstype =text / javascript>< / script>
photos.php
$(。cmdclose)。click(function(){
var id = this.id;
$ .post(cmddel。 php,{id:id});
setTimeout(function(){
<?php echo'var id ='.json_encode($ _ GET ['id'])。'; ';>
$('#cmdz')。load('cmdajax.php?id ='+ id);
},1000);
}) ;
html部分:
< div id =cmdz>
<?php
$ uid = $ _ SESSION ['uid'];
$ query =SELECT *
从`cmds`
WHERE`pid` = $ {id} ORDER BY`id`;
$ result = $ db-> query($ query);
?>
< table id =tblcmd>
< tr>
while($ result_set = $ db-> fetch($ result)){
// echo $ result_set ['cmds'];
$ uid = $ result_set ['uid'];
$ query2 =SELECT * FROM`user` WHERE`id` ='$ {uid}';
$ result2 = $ db-> query($ query2);
$ result_set2 = $ db-> fetch($ result2);
echo'< td>'。$ result_set2 ['name']。'< / td>';
echo'< td>:'。$ result_set ['cmds']。'< / td>';
echo'< td id ='。$ result_set ['id']。'>< img src =images / cmd-close.jpgclass =cmdcloseid ='。 $ result_set ['id']。'/> < / TD>;
?>
< / tr>
<?php
}
?>
< / table>
< / div>
cmdajax.php
<?php
session_start();
require_once(includes / database.php);
if(isset($ _ GET ['id'])){
$ id = $ _ GET ['id'];
}
$ uid = $ _ SESSION ['uid'];
$ query =SELECT * FROM`cmds` WHERE`pid` = $ {id} ORDER BY`id`;
$ result = $ db-> query($ query);
?>
< table id =tblcmd>
< tr>
while($ result_set = $ db-> fetch($ result)){
// echo $ result_set ['cmds'];
$ uid = $ result_set ['uid'];
$ query2 =SELECT * FROM`user` WHERE`id` ='$ {uid}';
$ result2 = $ db-> query($ query2);
$ result_set2 = $ db-> fetch($ result2);
echo'< td>'。$ result_set2 ['name']。'< / td>';
echo'< td>:'。$ result_set ['cmds']。'< / td>';
echo'< td id ='。$ result_set ['id']。'>< img src =images / cmd-close.jpgclass =cmdcloseid ='。 $ result_set ['id']。'/> < / TD>;
echo'< td>< abbr class =timeagotitle =2008-07-17T09:24:17Z> 2008年7月17日< / abbr>< / td>';
?>
< / tr>
<?php
}
?>
< / table>
我的问题是,在页面刷新或Ajax调用后,函数 $(。cmdclose)。click(function()
不能工作,为什么不能?///解决这个问题
现在我我正面临这个问题:
jQuery(abbr.timeago)。timeago();
这个选择器不能正常工作什么是共振?谢谢
因为您在的功能中单击
新的内容被注入到DOM中,所以这些功能将不能用于新添加的(注入的)动态内容,为了解决这个问题,你需要使用jQuery
因此,将您的代码从 $ b <$ p $点击(功能(){
//您的代码
}); $ b $($); $ b $(。cmdclose b});
至
$(function(){
$(document).on(click,。cmdclose,function(){
// your code
});
});
jQuery on
将适用于当前和未来元素。 on
是jQuery 1.7以上的版本。如果您使用的是以前版本的jQuery,请使用 live
headers :
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="js/timeago.js" ></script>
<script src="js/test_helpers.js" type="text/javascript"></script>
photos.php
$(".cmdclose").click(function(){
var id=this.id;
$.post("cmddel.php" ,{"id":id});
setTimeout(function(){
<?php echo 'var id = '.json_encode($_GET['id']).';'; ?>
$('#cmdz').load('cmdajax.php?id='+id);
},1000);
});
html part:
<div id="cmdz">
<?php
$uid=$_SESSION['uid'];
$query="SELECT *
FROM `cmds`
WHERE `pid` =${id} ORDER BY `id`";
$result=$db->query($query);
?>
<table id="tblcmd">
<tr>
<?
while($result_set=$db->fetch($result)){
// echo $result_set['cmds'];
$uid=$result_set['uid'];
$query2="SELECT * FROM `user` WHERE `id` ='${uid}'";
$result2=$db->query($query2);
$result_set2=$db->fetch($result2);
echo '<td>'.$result_set2['name'].'</td>';
echo '<td>:'.$result_set['cmds'].'</td>';
echo '<td id="'.$result_set['id'] .'"><img src="images/cmd-close.jpg" class="cmdclose" id="'.$result_set['id'] .'" /> </td>';
?>
</tr>
<?php
}
?>
</table>
</div>
cmdajax.php
<?php
session_start();
require_once("includes/database.php");
if(isset($_GET['id'])){
$id=$_GET['id'];
}
$uid=$_SESSION['uid'];
$query="SELECT * FROM `cmds` WHERE `pid` =${id} ORDER BY `id`";
$result=$db->query($query);
?>
<table id="tblcmd">
<tr>
<?php
while($result_set=$db->fetch($result)){
// echo $result_set['cmds'];
$uid=$result_set['uid'];
$query2="SELECT * FROM `user` WHERE `id` ='${uid}'";
$result2=$db->query($query2);
$result_set2=$db->fetch($result2);
echo '<td>'.$result_set2['name'].'</td>';
echo '<td>:'.$result_set['cmds'].'</td>';
echo '<td id="'.$result_set['id'] .'"><img src="images/cmd-close.jpg" class="cmdclose" id="'.$result_set['id'] .'" /> </td>';
echo '<td><abbr class="timeago" title="2008-07-17T09:24:17Z">July 17, 2008</abbr></td>';
?>
</tr>
<?php
}
?>
</table>
My problem is that, after one page refresh or Ajax call, the function $(".cmdclose").click(function()
will not work. Why not?/// this problem resolved
now i am facing this problem:
jQuery("abbr.timeago").timeago(); this selector won't work properly what will be the reson? Thanks
Because you bounded the functionality of the click
event even before the new content is injected to the DOM. So that functionality will not be available to the newly added (injected) dynamic content. To handle this , you need to use jQuery on
So Change your code from
$(function(){
$(".cmdclose").click(function(){
// your code
});
});
to
$(function(){
$(document).on("click",".cmdclose",function(){
//your code
});
});
jQuery on
will work for current and future elements. on
is avaialbe from jQuery 1.7+ onwards. If you are using a previous version of jQuery, use live
这篇关于ajax调用jquery函数后无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!