gpt4 book ai didi

javascript - Jquery 父级无法正常工作

转载 作者:太空宇宙 更新时间:2023-11-04 11:20:14 26 4
gpt4 key购买 nike

前几天(勉强)做了一个评论系统,想添加EditReplyDelete功能,并且在搜索网络(包括这个)之后我成功了,但现在我遇到了 jQuery 问题。我想用它来创建一些按钮,单击这些按钮将显示 ReplyDeleteEdit 表单,所以我搜索了这个论坛并找到了一些解决方案,并应用了它们,但我遇到了各种问题:

首先是当我使用.toggle时,它会显示所有评论的表单,然后我开始使用parents(),但是当我使用这个时什么都没有有效!

代码如下:

<body>
<h1>Comments (3)</h1>

<div class="comment">
<form action="new.php" id="new_comment" method="post" name=
"new_comment">
<textarea class="text_cmt" name="text_cmt" placeholder=
"Post a new comment">
</textarea><br>
<input type="submit" value="Post">
</form>
</div>

<div class="comment">
<span class="author">Guest</span><br>
hhhhhhhhhhhhhhhhhhhh<br>
<span class="date">Posted: Sep 27, 2015 | 12:55:48</span><br>
<span class="replies">[+] 1 replies</span><span class="replies"></span>

<form action="delete.php" id="delete" method="post" name="delete">
<input id="password" name="password" placeholder=
"Password to delete" type="text"><input type="submit" value=
"Delete">
</form><input class="replytopic" name="send" type="submit" value=
"Reply"><button class="button2">Edit</button><button class=
"button3">Delete</button>

<div id="reply">
<form action="reply.php" class="reply" id="new_comment" method=
"post" name="new_comment">
<input name="par_code" type="hidden" value="ySWxTshchM">
<textarea class="text_cmt" name="text_cmt" placeholder=
"Reply to Guest">
</textarea><br>
<input type="submit" value="Reply">
</form>
</div>

<div id="children">
<form action="edit.php" id="edit" method="post" name="edit">
<textarea class="text_cmt" name="text_cmt">
</textarea><br>
<input id="password" name="password" placeholder=
"Password to edit" type="text"><input type="submit" value=
"Edit">
</form>

<div class="child">
<span class="author">k86k</span><br>
gstrdynbv<br>
<span class="date">Posted: Sep 25, 2015 | 00:00:00</span><br>
</div>
</div>
</div>

<div class="comment">
<span class="author">Guest</span><br>
jjjjj<br>
<span class="date">Posted: Sep 27, 2015 | 12:41:11</span><br>
<span class="replies">[+] 1 replies</span><span class="replies"></span>

<form action="delete.php" id="delete" method="post" name="delete">
<input id="password" name="password" placeholder=
"Password to delete" type="text"><input type="submit" value=
"Delete">
</form><input class="replytopic" name="send" type="submit" value=
"Reply"><button class="button2">Edit</button><button class=
"button3">Delete</button>
</div>
</body>

<script>
$(function() {
$(".replytopic").on('click', function() {
$(this).parent().next(".reply").toggle();
});
})
</script>
<script>
$( ".button3" ).click(function() {
$( "form#delete" ).toggle();
});
</script>
<script>
$(function() {
$(".replytopic").on('click', function() {
$(this).parent().next("form#reply").toggle();
});
})
</script>

这是我的显示/隐藏发生了什么的演示:jsfiddle我还想提一下,这个页面是由 index.php 生成的:

<?php
get_comments('1234');
?>

<script>
$(function() {
$(".replytopic").on('click', function() {
$(this).parent().next(".reply").toggle();
});
})
</script>
<script>
$( ".button3" ).click(function() {
$( "form#delete" ).toggle();
});
</script>
<script>
$(function() {
$(".replytopic").on('click', function() {
$(this).parent().next("form#reply").toggle();
});
})
</script>

和我的 PHP functions.php

最佳答案

你不需要有同名的 id 值,所以这些值都需要切换到 class 。您还应该尝试包装您的删除表单,以便您可以轻松地从单击的按钮向上遍历到包含元素。我专门做了删除按钮,所以如果有任何其他问题,您应该能够应用相同的原则。

演示:http://jsfiddle.net/0oky70dd/

CSS:

body {
font-family: 'Open Sans', sans-serif;
font-size: 13px;
}
/* comments */
.comment {
width: 45%;
height: auto;
padding: 7px 23px;
background-color: #F5F5F5;
box-shadow: 0px 2px 4px rgba(0,0,0,0.15), 0px 0px 3px rgba(0,0,0,0.15);
margin: 0px auto;
margin-bottom: 10px;
text-align: justify;
}
.child {
width: calc(100% - 92px);
height: auto;
padding: 3px 25px;
margin: 10px 0px 10px 35px;
box-shadow: -5px 0px 0px 0px #777;
background-color: rgba(1,1,1,0.05);
border: 1px solid rgba(1,1,1,0.1);
}
/* fonts */
.author, .date {
font-size: 11.2px;
font-weight: 600;
color: #777;
text-transform: uppercase;
}
.replies {
color: #39C;
font-weight: 600;
cursor: pointer;
}
.replies:hover {
color: #39F;
}
.text_cmt {
width: calc(100% - 22px);
height: auto;
padding: 10px;
background-color: #FFF;
border: 1px solid rgba(1,1,1,0.3);
outline: none;
font-family: 'Open Sans', sans-serif;
font-size: 13px;
line-height: 15px;
margin-top: 10px;
}
h1 {
width: 45%;
height: auto;
margin: 0px auto;
font-size: 42px;
font-weight: normal;
}
#hide {
}
#children {
}
#item {
height: 40px;
width: 30px;
background-color: red;
}
button.edittopic {
float: right;
color: white;
background-color: #3cb0fd;
border-top-style: none;
border-right-style: none;
border-bottom-style: none;
border-left-style: none;
padding: 2px 10px 2px 10px;
margin-left: 5px;
}
button.button2 {
background-color: #2FC332;
float: right;
margin-left: 5px;
border: 0px none;
padding: 2px 10px;
color: #FFF;
}
button.delete_button {
background-color: #D60202;
float: right;
margin-left: 5px;
border: 0px none;
padding: 2px 10px;
color: #FFF;
}
form.form_delete {
width: 35.5%;
display: none;
}
form#edit {
display: none;
}
form.reply {
display: none;
}

Javascript:

$(function() {
$(".edittopic").on('click', function() {
$(this).parent().next(".reply").toggle();
});

$( ".delete_button" ).click(function() {
$(this).parents(".delete_wrapper").find(".form_delete").toggle();
});

$(".edittopic").on('click', function() {
$(this).parent().next("form#reply").toggle();
});
})

HTML:

<h1>Comments (3)</h1>
<div class="comment">
<form action="new.php" method="post" enctpye="" name="new_comment">
<textarea class="text_cmt" name="text_cmt" placeholder="Post a new comment"></textarea>
<br />
<input type="submit" value="Post" />
</form>
</div>
<div class="comment" name="ySWxTshchM"><span class="author">Guest</span><br />
hhhhhhhhhhhhhhhhhhhh<br />
<span class="date">Posted: Sep 27, 2015 | 12:55:48</span><br />
<span class="replies">[+] 1 replies</span><span class="replies">Reply</span>
<div class="delete_wrapper">
<form action="delete.php" method="post" enctpye="" class="form_delete">
<input name="password" type="text" placeholder="Password to delete" id="password">
<input type="submit" value="Delete" />
</form>
<input type="submit" value="Reply" class="replytopic" name="send" />
<button class="button2">Edit</button>
<button class="delete_button">Delete</button>
</div>
<div id="reply">
<form class="reply" action="reply.php" method="post" enctpye="" name="new_comment">
<input type="hidden" name="par_code" value="ySWxTshchM" />
<textarea class="text_cmt" name="text_cmt" placeholder="Reply to Guest"></textarea>
<br />
<input type="submit" value="Reply" />
</form>
</div>
<div name="children" id="children">
<form action="edit.php" method="post" enctpye="" id="edit">
<textarea class="text_cmt" name="text_cmt"></textarea>
<br />
<input name="password" type="text" placeholder="Password to edit" id="password">
<input type="submit" value="Edit" />
</form>
<div class="child" name="tsrdyj5645w3"><span class="author">k86k</span><br />
gstrdynbv<br />
<span class="date">Posted: Sep 25, 2015 | 00:00:00</span><br />
</div>
</div>
</div>
<div class="comment" name="F6aBYrq5wG"><span class="author">Guest</span><br />
jjjjj<br />
<span class="date">Posted: Sep 27, 2015 | 12:41:11</span><br />
<span class="replies">[+] 1 replies</span><span class="replies">Reply</span>
<div class="delete_wrapper">
<form action="delete.php" method="post" enctpye="" class="form_delete">
<input name="password" type="text" placeholder="Password to delete" id="password">
<input type="submit" value="Delete" />
</form>
<input type="submit" value="Reply" class="replytopic" name="send" />
<button class="button2">Edit</button>
<button class="delete_button">Delete</button>
</div>
<div id="reply">
<form class="reply" action="reply.php" method="post" enctpye="" name="new_comment">
<input type="hidden" name="par_code" value="F6aBYrq5wG" />
<textarea class="text_cmt" name="text_cmt" placeholder="Reply to Guest"></textarea>
<br />
<input type="submit" value="Reply" />
</form>
</div>
<div name="children" id="children">
<form action="edit.php" method="post" enctpye="" id="edit">
<textarea class="text_cmt" name="text_cmt"></textarea>
<br />
<input name="password" type="text" placeholder="Password to edit" id="password">
<input type="submit" value="Edit" />
</form>
<div class="child" name="gr425g4"><span class="author">hhhh</span><br />
reeeeee<br />
<span class="date">Posted: Sep 23, 2015 | 00:00:00</span><br />
</div>
</div>
</div>
<div class="comment" name="TsA9y9hdvZ"><span class="author">Guest</span><br />
kkkk<br />
<span class="date">Posted: Sep 26, 2015 | 16:00:39</span><br />
<span class="replies">[+] 1 replies</span><span class="replies">Reply</span>
<div class="delete_wrapper">
<form action="delete.php" method="post" enctpye="" class="form_delete">
<input name="password" type="text" placeholder="Password to delete" id="password">
<input type="submit" value="Delete" />
</form>
<input type="submit" value="Reply" class="replytopic" name="send" />
<button class="button2">Edit</button>
<button class="delete_button">Delete</button>
</div>
<div id="reply">
<form class="reply" action="reply.php" method="post" enctpye="" name="new_comment">
<input type="hidden" name="par_code" value="TsA9y9hdvZ" />
<textarea class="text_cmt" name="text_cmt" placeholder="Reply to Guest"></textarea>
<br />
<input type="submit" value="Reply" />
</form>
</div>
<div name="children" id="children">
<form action="edit.php" method="post" enctpye="" id="edit">
<textarea class="text_cmt" name="text_cmt"></textarea>
<br />
<input name="password" type="text" placeholder="Password to edit" id="password">
<input type="submit" value="Edit" />
</form>
<div class="child" name="ftdre76igu"><span class="author">kkkk</span><br />
reply<br />
<span class="date">Posted: Sep 16, 2015 | 00:00:00</span><br />
</div>
</div>
</div>

关于javascript - Jquery 父级无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32855032/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com