gpt4 book ai didi

php - jquery 类,打开两个相同的

转载 作者:可可西里 更新时间:2023-11-01 08:21:22 25 4
gpt4 key购买 nike

好的,我们有一个 mySQL while 循环回显特定结果的 div。

这是我们的代码:

<div class="post">
<p>hello World</p>
<div class="contentSlide">
<p>Members image on left and comments on the right. Date floated on right, and link to user.</p>
<br />
<div class="contact">
<h3>Post a comment below</h3>
<p>My <label class="agentsHcard">Listings</label> <small>Only displays for logged in</small></p>

<form>
<input type="text" READONLY></input><br />
<textarea id="testTextarea" class="" type="text" name="comment" rows="3" style="background: none repeat scroll 0% 0% rgb(255, 255, 255); border: 1px solid rgb(51, 153, 255);"></textarea><br />
<div style="float:right;margin-right:40px;">
<button id="buttonsend" class="medium pink button" value="submit" name="submit" type="submit">Add Comment</button>
</div>
</form>
<br />
</div>
</div>
<div id="button" class="open">Comments(6) &#x25bc;</div>
</div>

基本上每个结果在我们的数据库中都有一个唯一的ID,例如:1, 2, 3, 4, 5

$(document).ready(function(){
reply=false;
$(".close").live('click',function(){
if(reply!==true){
$(".contentSlide, #contact").slideUp();
$(this).html("Comments(6) \u25bc").removeClass("close").addClass("open");
}
});
$(".open").live('click',function(){
$(".contentSlide").slideDown();
$(this).html("Close \u25b2 <span style='float:right;' onclick='reply=true;' class='reply' >Post a <a href='javascript:void(0);'>comment</a></span>").removeClass("open").addClass("close");
});
$(".reply").live('click',function(e){

$("#contact").slideDown(function(){reply=false;});
$("#button").html("Post and Close \u25b2").removeClass("open").addClass("close");
});
})

我们正在寻找一种方法来做到这一点,以便为每个 div 分配一个唯一的 ID,该 ID 将传递给 javascript 并打开相关的 div。

就目前而言,如果我们有 2 个这样的结果,它们都会在单击打开类时打开(很明显)。

我们知道如何通过复制 JS 并为每个 JS 赋予一个特殊的 ID 来做到这一点,但我们正在寻找一种更简洁的方法,即只使用 JS 一次并将其设为全局。

我们怎样才能让我们只有一段 javascript,并且它运行一个 ID?例如:

<div class="contentSlide" id="4">

谢谢。

最佳答案

如果你想要一个单独的 Javascript,那么 id 可能不是合适的方法。为了对与被点击元素相关的DOM元素进行操作,你可能需要使用相对遍历函数,例如closest(selector)向上导航到当前目标的“.post”div,然后向下遍历到要操作的元素:

$(".close").live('click',function(){
if(reply!==true){
$(this).closest(".post").children(".contentSlide, #contact").slideUp();
$(this).html("Comments(6) \u25bc").removeClass("close").addClass("open");
}
});

如果您确实使用了基于 Id 的机制,您可能需要进行一些字符串操作,并让元素 id 成为带有前缀/后缀的原始 id(即 button_4、contact_4 等)

关于php - jquery 类,打开两个相同的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7707728/

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