Javascript 打开特定div的弹出窗口
当我点击Javascript 打开特定div的弹出窗口,javascript,jquery,Javascript,Jquery,当我点击id=“team-1”的class=“team single”时,它应该会打开该特定id的团队弹出窗口 但它似乎不起作用 <div class="team-single" id="team-1"> <div class="team-popup"> <span class="close-btn">x close</span> </div> </div> <div class=
id=“team-1”
的class=“team single”
时,它应该会打开该特定id的团队弹出窗口
但它似乎不起作用
<div class="team-single" id="team-1">
<div class="team-popup">
<span class="close-btn">x close</span>
</div>
</div>
<div class="team-single" id=team-2>
<div class="team-popup">
<span class="close-btn">x close</span>
</div>
</div>
我会减少你必须做的只是:
jQuery(".team-single").click(function(e) {
jQuery(this).find('div.team-popup').show();
});
我会减少你必须做的只是:
jQuery(".team-single").click(function(e) {
jQuery(this).find('div.team-popup').show();
});
使用find()
和this
获取当前上下文
jQuery(".team-single").click(function(e){
jQuery(this).find(".team-popup").css({ display :"block",
});
//Or
// jQuery(".team-popup",this).css({ display :"block",
});
});
代码不起作用的原因:
您将ID存储在变量中,要在选择器中访问此变量,请使用:
jQuery("#"+currentID+" .team-popup").
使用find()
和this
获取当前上下文
jQuery(".team-single").click(function(e){
jQuery(this).find(".team-popup").css({ display :"block",
});
//Or
// jQuery(".team-popup",this).css({ display :"block",
});
});
代码不起作用的原因:
您将ID存储在变量中,要在选择器中访问此变量,请使用:
jQuery("#"+currentID+" .team-popup").
变量不会在字符串中被替换。如果要在字符串中使用变量,必须使用串联:
$("#" + currentID + " .team-popup")
但是使用$(this.find()
的答案是更好的解决方案。我发布这篇文章是为了让您了解代码的错误。字符串中不替换变量。如果要在字符串中使用变量,必须使用串联:
$("#" + currentID + " .team-popup")
但是使用$(this.find()
的答案是更好的解决方案。我只是发布此消息,以便您了解代码的错误。使用此命令:
jQuery(".team-single").click(function(e){
$(this).children().css("display", "block");
});
使用以下命令:
jQuery(".team-single").click(function(e){
$(this).children().css("display", "block");
});
什么是#currentID
?$(此)。查找(.team popup”)
您的第二个id缺少周围的引号。什么是#currentID
?$(此)。查找(.team popup”)
您的第二个id缺少周围的引号。感谢您的解释。这将对未来的解释非常有帮助。这在将来会很有帮助