我有3个锚标签,点击后我想在页面上显示一个div。然后,如果单击另一个锚标签,我想隐藏当前的div (在页面下方显示的那个)。到目前为止,我的代码如下所示:
$(document).ready(function () {
$('.text-show-1').on('click', function (e) {
e.preventDefault();
$('.container-show-1').removeClass('hide');
});
text-show-1是被点击的内容,container-show-1是被点击时显示的内容。
我在语句中尝试了我自己的if语句,结果是这样的--但这不起作用。
$(document).ready(function () {
$('.text-show-1').on('click', function (e) {
e.preventDefault();
$('.container-show-1').removeClass('hide');
if ($('.text-show-2').on('click', function () {
$('.container-show-1').addClass('hide');
}))
if else($('.text-show-3').on('click', function () {
$('.container-show-1').addClass('hide');
}))
});
任何帮助都将不胜感激。干杯!
发布于 2017-02-13 10:30:16
是否使用.show()
、.hide()
或.toggle()
由您决定。请阅读jQuery documentation以作出决定。
在功能方面,不要将点击事件嵌套在其他点击事件中。它们永远不会运行,因为只有当您单击第一个元素时,它们才可能运行,但您不能同时单击两个元素--明白我的意思吗?
有三个单独的单击事件,每个事件都会导致不同的操作:
$(document).ready(function () {
$('.text-show-1').on('click', function (e) {
e.preventDefault();
$('.container-show-1').show();
});
$('.text-show-2').on('click', function (e) {
e.preventDefault();
$('.container-show-1').hide();
//should you also be showing other things?
});
$('.text-show-3').on('click', function (e) {
e.preventDefault();
$('.container-show-1').hide();
});
});
发布于 2017-02-13 10:24:11
如果您使用jQuery
要隐藏元素,请使用$(".container-show-1").hide();
$(document).ready(function () {
$('.text-show-1').on('click', function (e) {
e.preventDefault();
$('.container-show-1').show();
});
$('.text-show-2').on('click', function () {
$('.container-show-1').hide();
});
$('.text-show-3').on('click', function () {
$('.container-show-1').hide();
});
});
不确定这是否是您期望看到的。
发布于 2017-02-13 10:26:59
尝试如下所示:
$("button").click(function(){ $("div").hide(1000); });
https://stackoverflow.com/questions/42195715
复制相似问题