我知道以前曾问过类似的问题,但没有一个适合我的需要。 我有一个XHR ajax返回,我想在php循环中放置尊重div类,ajax调用成功,但是当我使用jquery用div上的ajax返回替换我的div类html时,它取代了所有的循环中的div。请问我做错了什么:
我的循环PHP代码如下;
public function getComments() {
try{
$user_data = @$_SESSION['user'];
$userid = $user_data['userid'];
$name = $user_data['name'];
$con = new PDO("mysql:host=$this->serverhost;dbname=starstv;" , $this->serverusername, $this->serverpassword);
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$fetch_p_comment = $con -> prepare("SELECT parent_comments.name,parent_comments.likes,parent_comments.dislikes,parent_comments.comment,users.profile_pics,parent_comments.comment_time,parent_comments.code FROM parent_comments INNER JOIN users ON parent_comments.name = users.name ORDER BY comment_time DESC");
$fetch_p_comment -> execute();
$p = $fetch_p_comment -> fetchALL(PDO::FETCH_ASSOC);
foreach($p as $item) {
$item[] = $p;
$date = new dateTime($item['comment_time']);
$comment_time = date_format($date, 'M j, Y | H:i:s');
$name = $item['name'];
$comment = $item['comment'];
$par_code = $item['code'];
$user_image = $item['profile_pics'];
$likes = $item['likes'];
$dislikes = $item['dislikes'];
echo '<div id="parent-comment"></div>';
echo "<div class='comment' id='parent' name='".$par_code."'>"
."<img src='".$user_image."' alt='profile image'>"
."<span class='user' id ='name'>".$name."</span>"
."<span class='time'>".$comment_time."</span>"
.'<div class="comment-text"><p class="position-comments">'.$comment.'</p></div>'
."<img src='images/like.jpg' class='likes-mama' name=".$par_code." id='okay'><span class='likes-mama-updates' id='ccc-likes'> ".$likes."</span><img src='images/dislike.jpg' class='dislikes-mama' id='okay' name=".$par_code."><span class='dislikes-mama-updates' id='ccc-dislikes'> ".$dislikes."</span><br>"
."<a class='link-reply' id='reply' name='".$par_code."' value='".$par_code."' title='reply comment'>reply</a> <a class='edit-comment' id='edit' name='".$par_code."' value='".$par_code."' title='you can only edit your comment'>edit</a> <a class='delete-comment' id='delete' name='".$par_code."' value='".$par_code."'title='you can only delete your comment'>delete</a><br><br>";
$fetch_c_reply = $con -> prepare("SELECT children_comments.name,children_comments.likes,children_comments.dislikes,children_comments.comment,users.profile_pics,children_comments.comment_time,children_comments.par_code FROM children_comments INNER JOIN users ON children_comments.name = users.name WHERE children_comments.par_code =? ORDER BY comment_time DESC");
$fetch_c_reply -> bindParam(1,$par_code);
if($fetch_c_reply -> execute()) {
$c = $fetch_c_reply -> fetchALL(PDO::FETCH_ASSOC);
$c_count = $fetch_c_reply->rowCount();
if($c_count == 0) {
// do nothing
}else {
echo "<a class='link-reply' id='children' name='".$par_code."'><img src='images/images(39).jpg' alt='' id='kiop' title='toggle to see replies'>(".$c_count.") <img src='images/images(39).jpg' alt='' id='kioper' title='toggle to see replies'><div id='form-ajax'></div></a>"
."<div class='child-comments' id='C-".$par_code."'>";
foreach($c as $com) {
$com[] = $c;
$c_date = new dateTime($com['comment_time']);
$c_comment_time = date_format($c_date, 'M j, Y | H:i:s');
$c_name = $com['name'];
$c_comment = $com['comment'];
$c_par = $com['par_code'];
$image = $com['profile_pics'];
$likes = $com['likes'];
$dislikes = $com['dislikes'];
echo '<div id="parent-comment"></div>';
echo "<div class='child' id='".$par_code."-C' value='".$par_code."'>"
."<img src='".$image."' alt='profile image'>"
."<span class='user'>".$c_name."</span>"
."<span class='time'>".$c_comment_time."</span>"
."<div class='comment-text'><p class='position-comments'>".$c_comment."</p></div>"
."<img src='images/like.jpg' class='c-likes' name=".$par_code." value=".$c_comment." id='okay'><span > ".$likes."</span><img src='images/dislike.jpg' class='c-dislikes' id='okay' value=".$c_comment." name=".$par_code." ><span > ".$dislikes."</span><br>"
."<div></div>"
."</div>";
}
echo "</div>";
}
}
echo "</div>";
}
}catch(PDOException $e) {
throw new PDOException($e->getMessage());
}
}
这是完整的PHP代码返回循环;
php循环代码如下;
echo "<div class='comment' id='parent' name='".$par_code."'>"
."<img src='".$user_image."' alt='profile image'>"
."<span class='user' id ='name'>".$name."</span>"
."<span class='time'>".$comment_time."</span>"
.'<div class="comment-text"><p class="position-comments">'.$comment.'</p></div>'
."<img src='images/like.jpg' class='likes-mama' name=".$par_code." id='okay' ><span class='likes-mama-updates' id='ccc-likes'> ".$likes."</span><img src='images/dislike.jpg' class='dislikes-mama' id='okay' name=".$par_code."><span class='dislikes-mama-updates' id='ccc-dislikes'> ".$dislikes."</span><br>"
."<a class='link-reply' id='reply' name='".$par_code."' value='".$par_code."' title='reply comment'>reply</a> <a class='edit-comment' id='edit' name='".$par_code."' value='".$par_code."' title='you can only edit your comment'>edit</a> <a class='delete-comment' id='delete' name='".$par_code."' value='".$par_code."' title='you can only delete your comment'>delete</a></div>";
我的javascript代码如下;
$(".likes-mama").one('click', function() {
var p_comment_likes = true;
var gCode = $(this).attr("name");
if(gCode != "" && p_comment_likes ==true) {
var hr = new XMLHttpRequest();
hr.open("POST","p_comment_likes_dislikes.php",true);
hr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
hr.onreadystatechange = function () {
if((hr.readyState == 4) && (hr.status == 200 || hr.status == 304) ) {
$(".likes-mama-updates").html(hr.response);
alert(hr.response);
}
}
hr.send("code="+gCode+"&p_comment_likes="+p_comment_likes);
}
});
请您的回复表示赞赏
答案 0 :(得分:0)
经过大量的工作,我能够在jquery中找到带有父子关系的解决方案:
$(this).parent().children("my-class");
找到我想要的实际div。 谢谢大家的快速回复!