删除注释而不刷新页面
Delete comment without page refresh
我想知道如何改进此代码,以便在删除注释时不刷新页面。如果你在页面的一半删除了一条评论,它又会回到顶部,这会很烦人。
AJAX
function delete_(pid){
$.ajax({
type: "POST",
url: "../include/post.delete.php",
data: "pid="+pid,
success: function(){
}
});
}
DELETEPOST
echo'<div class="userinfo"><div id="divider">';
if($streamitem_data['streamitem_type_id'] == 1||$streamitem_data['streamitem_type_id'] == 3){
echo "";
}else{
echo "";
}
$poster_name = _user_core::getuser($streamitem_data['streamitem_creator']);
$target_name = _user_core::getuser($streamitem_data['streamitem_target']);
if($streamitem_data['streamitem_type_id'] == 1||$streamitem_data['streamitem_type_id'] == 3){
if(!($streamitem_data['streamitem_type_id']==1)) {
echo "<a href='/profile.php?username=".$poster_name['username']."'><img class='stream_profileimage' style='border:none;padding:0px;display:inline;' border='"0'" src='"imgs/cropped".$streamitem_data['streamitem_creator'].".jpg'" onerror='this.src='"img/no_profile_img.jpeg'"' width='"40'" height='"40'" ></a>";
echo "<a href='/profile.php?username=".$target_name['username']."'><img class='stream_profileimage' style='border:none;padding:0px;display:inline;' border='"0'" src='"imgs/cropped".$streamitem_data['streamitem_target'].".jpg'" onerror='this.src='"img/no_profile_img.jpeg'"' width='"40'" height='"40'" ></a>";
}
if(!($streamitem_data['streamitem_type_id']==3)) {
echo "<a href='/profile.php?username=".$poster_name['username']."'><img class='stream_profileimage' style='border:none;padding:0px;display:inline;' border='"0'" src='"imgs/cropped".$streamitem_data['streamitem_target'].".jpg'" onerror='this.src='"img/no_profile_img.jpeg'"' width='"40'" height='"40'" ></a>";
}
$cont = stripslashes($streamitem_data['streamitem_content']);
if(!($streamitem_data['streamitem_type_id']==1)){
//$cont = htmlentities($cont);
$cont = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a class='user_link' href='"''0'">''0</a>", $cont);
}
if($streamitem_data['streamitem_creator']==$streamitem_data['streamitem_target']){
echo "<a href='/profile.php?username=".$poster_name['id']."'>" . $poster_name['fullusersname']."</a><span class='subtleLink'> said </span>";
}else{
echo "<a href='profile.php?username=".$poster_name['username']."'>" .$poster_name['fullusersname']."</a>
<span class='subtleLink'>wrote on </span>
<a href='profile.php?username=".$poster_name['username']."'>" .$target_name['fullusersname']." 's</a> stream";
}
echo "<br/><a href='#' class='subtleLink' style='font-weight:normal;'>".Agotime($streamitem_data['streamitem_timestamp'])."</a></div><hr><div style='padding-left:10px;padding-right:10px;'>";
if($streamitem_data['streamitem_type_id']==1){
$cont = nl2br($cont);
echo "<span class='subtleLink'>".$cont."</span>";
}else{
if($streamitem_data['streamitem_creator']==$streamitem_data['streamitem_target']){
$cont = nl2br($cont);
echo "<div>".$cont."</div>";
}else{
$cont = nl2br($cont);
echo "<div>'".$cont."</div>";
}
}
echo "</div><div class='post_contextoptions'>";
if($streamitem_data['streamitem_creator']==$_SESSION['id']){
echo "<div class='stream_option' id='delete_option'><form style='display:inline;' id='delete_".$streamitem_data['streamitem_id']."' action='include/post.delete.php' method='POST' target='ifr2'><input type='hidden' name='pid' value='".$streamitem_data['streamitem_id']."'><a style='cursor:pointer;position:relative;top:-70px;float:right;padding-right:5px;' onClick='document.forms['"delete_".$streamitem_data['streamitem_id']."'"].submit();refreshstream();'>X</a></div>";
}
您需要对刚刚删除的注释元素调用.remove()
。你需要弄清楚如何识别它。有一种方法:
function delete_(pid){
$.ajax({
type: "POST",
url: "../include/post.delete.php",
data: "pid="+pid,
success: function(){
// However you can identify the comment's div, and delete it by calling .remove()
// This is if it has a unique id
$("#comment-"+pid).remove();
}
});
}
否则,如果使用<a href="javascript:delete_(pid);">
,则可以添加类似<a href="javascript:delete_(pid, this);">
的parmeter。例如,通过这种方式,您可以通过$(the_element_you_passed_to_the_function).parents("div.the-comment-div-parent").remove()
访问该元素。
否则,如果使用此方法,则可以直接在$("a").click(function {});
中实现此函数。这取决于你。
相关文章:
- 强制模板刷新ember.js
- 如何通过ajax刷新JSF填充的javascript变量
- 如何解决Yii中的页面刷新问题
- Jquery提交表单而不刷新
- 刷新页面后会出现警报
- 刷新后保留对网页的更改
- 如何在不刷新页面的情况下更新显示框
- 重定向时角度刷新浏览器
- 如何防止jQueryonclick事件中的Ruby方法在页面刷新时执行
- Javascript,输出结果后页面不断刷新
- $(document).height()在刷新时随机化值(Safari 5.1.10)
- 刷新父窗口后无法关闭窗口
- 刷新页面时hasClass不起作用
- X秒后刷新select元素
- chart.series[id].remove()无法刷新高位图表/高位股票中其他系列的图例属性
- Javascript中的备选注释方法
- 触发媒体查询断点时刷新页面
- 如何刷新列表框内容onclick或blur事件
- 如何在火狐浏览器中禁用F5和刷新
- 删除注释而不刷新页面