gpt4 book ai didi

javascript - 如何在下拉 JQuery 中添加重新点击?

转载 作者:太空宇宙 更新时间:2023-11-04 10:28:01 25 4
gpt4 key购买 nike

我正在用 JQuery 创建我的第一个脚本,为了测试我的基础知识,我尝试在 JQuery 中做一个简单的下拉菜单。代码工作完美,但有一个问题,如果我重新单击按钮打开文本,如果文本打开,如果重新单击它应该关闭,这里的代码:

$( ".select1" ).click(function() {
$(".content1").css("display", "block");
});

$( ".select2").click(function() {
$(".content2").css("display", "block");
})

$(".select3").click(function() {
$(".content3").css("display", "block");
})

$(".select4").click(function() {
$(".content4").css("display", "block");
})
article {
display: none;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<title>JS Bin</title>
</head>
<body>
<div class="dropdown-wrapper">
<p class="select1">Select 1</p>
<article class="content1">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Molestiae mollitia aliquid quas doloremque, repudiandae vel odio nisi repellendus accusantium temporibus distinctio labore, debitis tenetur, in assumenda. Odio possimus pariatur, vitae!</article>
<p class="select2">Select 2</p>
<article class="content2">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quas iure, doloribus qui quasi est, quo obcaecati modi ab nesciunt pariatur a id rerum reprehenderit, beatae corrupti consequatur ut placeat ipsa!</article>
<p class="select3">Select 3</p>
<article class="content3">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Neque voluptate beatae placeat nobis porro, id ipsum. Incidunt minima deserunt, delectus. Ad unde obcaecati natus quo excepturi, tempore consectetur odio explicabo.</article>
<p class="select4">Select 4</p>
<article class="content4">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nihil praesentium ex ipsa dolor alias ratione repellat eos? Eveniet architecto officiis, veniam reiciendis animi, enim aperiam error dignissimos expedita laborum eius.</article>
</div>
</body>
</html>

最佳答案

您可以使用 .toggle() 显示和隐藏元素或 .toggleClass() .我选择了 .toggleClass(),因为它更容易从隐藏状态开始:

/** You need only one declaration using a common selector - class .select in this case **/
$( ".select" ).click(function() {
$(this).next().toggleClass('visibile'); // if clicked toggle the class on the next element (the article)
});
article:not(.visibile) { /** all articles that don't have the class visible, should be hidden **/
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<title>JS Bin</title>
</head>
<body>
<div class="dropdown-wrapper">
<p class="select">Select 1</p>
<article class="content">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Molestiae mollitia aliquid quas doloremque, repudiandae vel odio nisi repellendus accusantium temporibus distinctio labore, debitis tenetur, in assumenda. Odio possimus pariatur, vitae!</article>
<p class="select">Select 2</p>
<article class="content">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quas iure, doloribus qui quasi est, quo obcaecati modi ab nesciunt pariatur a id rerum reprehenderit, beatae corrupti consequatur ut placeat ipsa!</article>
<p class="select">Select 3</p>
<article class="content">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Neque voluptate beatae placeat nobis porro, id ipsum. Incidunt minima deserunt, delectus. Ad unde obcaecati natus quo excepturi, tempore consectetur odio explicabo.</article>
<p class="select">Select 4</p>
<article class="content">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nihil praesentium ex ipsa dolor alias ratione repellat eos? Eveniet architecto officiis, veniam reiciendis animi, enim aperiam error dignissimos expedita laborum eius.</article>
</div>
</body>
</html>

关于javascript - 如何在下拉 JQuery 中添加重新点击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36668833/

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