gpt4 book ai didi

jquery - 使用 JQuery 展开折叠 div

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

我正在尝试理解这个 JQuery代码,我需要这样做,我会使用其他类型的功能,但我需要遵循这种方式,我需要修复一些东西,当你点击 plus 时符号它应该展开并显示一些内容,plus符号必须变为 minus符号并更改颜色,这可以正常工作,当您现在再次单击 minus 时符号它应该折叠并且不显示任何内容,但是当您单击 minus 时,这不起作用。符号它折叠并再次展开,我是新来的 Sass他们在这里使用它,我不确定是否理解一些 Sass特别代码 &.expanded , 这是什么意思?我在我的 html 中找不到一个名为 expanded 的类, 但它在 JQuery 中使用。他们将 JQuery 代码放在 _Layout 文件中。

JQuery 函数($(".contact_item.expanded") ?? 是什么意思??):

<script>
/* expand */
$(".contact_item .head").click(function () {
$('.contact_item.expanded .head').next('div').slideUp();
$('.contact_item.expanded').removeClass('expanded');
$(this).parent('div').toggleClass('expanded');
$(this).next('div').slideDown();
});
</script>

这就是我说的Sass代码,我不是很理解&签名:

 .contact_item {
width: 100%;
border: 1px solid #f1e7e0;
background-color: #fcf6f5;
margin: 3px 0px;
float: left;

&.expanded {
.head .name {
color: #f60;
}

.head .name span {
border-color: #f60;
color: #f60;

&.plus {
display: none;
}

&.minus {
display: block;
}
}
}

.head {
.name {
font-family: "Tahoma";
color: rgb(100, 100, 100);
font-size: 11.1px;
text-transform: uppercase;
padding: 7px 15px;
cursor: pointer;
position: relative;

span {
position: absolute;
display: block;
width: 20px;
height: 20px;
border-radius: 50%;
border: 1px solid #706f6f;
text-align: center;
right: 15px;
top: 7px;
font-size: 18px;
line-height: 17px;

&.minus {
display: none;
}
}
}
}
}

这是一张图片,你可以看到CALIFORNIA OFFICE已展开,但是当您单击 minus 时它折叠并再次展开的符号...

enter image description here

更新

这是使用 f12 的 html,现在是 <div class="contact_item">已更改为 <div class="contact_item expanded">但在 Razor 中它不会发生:

       <div class="row">
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
@{ var locations = LocationLookup.GetLocations(); }
@{ int numloc = locations.Count / 2;}
@{ var newlist = locations.Take(numloc);}
@foreach (var loc in newlist)
{
<div class="contact_item ">
<div class="head">
<div class="name">@loc.Name office<span class="plus">+</span> <span class="minus">-</span></div>
</div>
<div class="info">
<ul>
<li>
<div class="icon"><img src="//lig.azureedge.net/public/UK/Content/Images/marker.png" alt=""></div>
@loc.Address @loc.Continued, @loc.City, @loc.PostCode, @loc.State
</li>
<li>
<div class="icon"><img src="//lig.azureedge.net/public/UK/Content/Images/phone.png" alt=""></div>
@loc.Phone
</li>
</ul>
<div class="clearfix"></div>
</div>
</div>
}
</div>
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
@for (int i = numloc; i < locations.Count; i++)
{
<div class="contact_item ">
<div class="head">
<div class="name">@locations[i].Name office<span class="plus">+</span> <span class="minus">-</span></div>
</div>
<div class="info">
<ul>
<li>
<div class="icon"><img src="//lig.azureedge.net/public/UK/Content/Images/marker.png" alt=""></div>
@locations[i].Address @locations[i].Continued, @locations[i].City, @locations[i].PostCode, @locations[i].State
</li>
<li>
<div class="icon"><img src="//lig.azureedge.net/public/UK/Content/Images/phone.png" alt=""></div>
@locations[i].Phone
</li>
</ul>
<div class="clearfix"></div>
</div>
</div>
}
</div>
</div>
</div>

This is the html code using F12 in the browser, in here there is
<section class="hidden-print" id="contact">
<div class="container">
<div class="row">
<div class="col-lg-3 col-md-3 col-sm-3"> </div>
<div class="col-lg-9 col-md-9 col-sm-9 white">
<div class="row">
<div class="col-lg-9 col-md-9 col-sm-8 col-xs-12">
<div class="title">Contact us</div>
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">



<div class="contact_item expanded">
<div class="head">
<div class="name">California office<span class="plus">+</span> <span class="minus">-</span></div>
</div>
<div class="info" style="display: block;">
<ul>
<li>
<div class="icon"><img alt="" src="//lig.azureedge.net/public/UK/Content/Images/marker.png"></div>
One Embarcadero Center Suite 500, San Fransisco, 94111, CA
</li>
<li>
<div class="icon"><img alt="" src="//lig.azureedge.net/public/UK/Content/Images/phone.png"></div>
(415)690-6214
</li>
</ul>
<div class="clearfix"></div>
</div>
</div>
<div class="contact_item">
<div class="head">
<div class="name">Florida office<span class="plus">+</span> <span class="minus">-</span></div>
</div>
<div class="info" style="display: none;">
<ul>
<li>
<div class="icon"><img alt="" src="//lig.azureedge.net/public/UK/Content/Images/marker.png"></div>
111 2nd Avenue NE Suite 1101, St. Petersburg, 33701, FL
</li>
<li>
<div class="icon"><img alt="" src="//lig.azureedge.net/public/UK/Content/Images/phone.png"></div>
(727) 578-2800
</li>
</ul>
<div class="clearfix"></div>
</div>
</div>
<div class="contact_item ">
<div class="head">
<div class="name">London office<span class="plus">+</span> <span class="minus">-</span></div>
</div>
<div class="info">
<ul>
<li>
<div class="icon"><img alt="" src="//lig.azureedge.net/public/UK/Content/Images/marker.png"></div>
Lloyd’s Building, Gallery 4 12 Leadenhall St., London, EC3V 1LP, England
</li>
<li>
<div class="icon"><img alt="" src="//lig.azureedge.net/public/UK/Content/Images/phone.png"></div>
(0207) 101-9395
</li>
</ul>
<div class="clearfix"></div>
</div>
</div>
</div>
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
<div class="contact_item">
<div class="head">
<div class="name">Ohio office<span class="plus">+</span> <span class="minus">-</span></div>
</div>
<div class="info" style="display: none;">
<ul>
<li>
<div class="icon"><img alt="" src="//lig.azureedge.net/public/UK/Content/Images/marker.png"></div>
4449 Easton Way 2nd Floor, Columbus, 43219, OH
</li>
<li>
<div class="icon"><img alt="" src="//lig.azureedge.net/public/UK/Content/Images/phone.png"></div>
(614)526-8754
</li>
</ul>
<div class="clearfix"></div>
</div>
</div>
<div class="contact_item">
<div class="head">
<div class="name">Pennsylvania office<span class="plus">+</span> <span class="minus">-</span></div>
</div>
<div class="info" style="display: none;">
<ul>
<li>
<div class="icon"><img alt="" src="//lig.azureedge.net/public/UK/Content/Images/marker.png"></div>
One International Plaza Suite 550, Philadelphia, 19113, PA
</li>
<li>
<div class="icon"><img alt="" src="//lig.azureedge.net/public/UK/Content/Images/phone.png"></div>
(215)554-6777
</li>
</ul>
<div class="clearfix"></div>
</div>
</div>
<div class="contact_item ">
<div class="head">
<div class="name">Texas office<span class="plus">+</span> <span class="minus">-</span></div>
</div>
<div class="info">
<ul>
<li>
<div class="icon"><img alt="" src="//lig.azureedge.net/public/UK/Content/Images/marker.png"></div>
950 Echo Lane Suite 200, Houston, 77024, TX
</li>
<li>
<div class="icon"><img alt="" src="//lig.azureedge.net/public/UK/Content/Images/phone.png"></div>
(281)653-6472
</li>
</ul>
<div class="clearfix"></div>
</div>
</div>
<div class="contact_item ">
<div class="head">
<div class="name">Washington office<span class="plus">+</span> <span class="minus">-</span></div>
</div>
<div class="info">
<ul>
<li>
<div class="icon"><img alt="" src="//lig.azureedge.net/public/UK/Content/Images/marker.png"></div>
1100 Dexter Avenue North Suite 100, Seattle, 98109, WA
</li>
<li>
<div class="icon"><img alt="" src="//lig.azureedge.net/public/UK/Content/Images/phone.png"></div>
(253)271-9692
</li>
</ul>
<div class="clearfix"></div>
</div>
</div>
</div>
</div>
</div>

最佳答案

首先,让我们尝试了解 jQuery 选择器和 SASS 的工作原理。

用于在 jQuery 中选择元素的行以美元符号 ($) 开头,它是 jQuery 的简写。

选择器是传递给这个函数的参数。它就像 CSS 选择器一样工作。

var elementsWithAppleClass = $(".apple");

将选择所有具有 CSS 类“apple”的 html 元素

另一件事是关于 SASS 的工作原理。

Sass 基本上是带有一堆语法糖的 CSS,因此它比普通 CSS 更容易编写。

SASS 允许您嵌套 CSS 选择器。例如:

.banana
{
.pear
{
/* this style applies to all elements with the pear class
inside an element with the banana class */
}
}

SASS 中的 & 运算符意味着你仍然引用上面的类,但是如果它也有后面的条件会发生什么。

.banana
{
&.pear
{
/* this applies to elements that have the banana class, but */
/* that also have the pear class */
}
}

您还可以使用它为嵌套的 CSS 状态编写样式。

.banana
{
&:hover
{
background-color:yellow;
cursor:pointer;
}
}

关于您眼前的问题:

为了解决您的问题,您所要做的就是检查您单击的元素是否已经展开,并据此制定您的逻辑。

    /* expand */
$(".contact_item .head").click(function () {

if($(this).parent('div').hasClass('expanded'))
{
//this one is already expanded. we just need to collapse it.
$(this).parent('div').toggleClass('expanded');
$(this).next('div').slideUp();
}
else
{
//this one is not yet expanded. let's collapse all the other ones
$('.contact_item.expanded .head').next('div').slideUp();
$('.contact_item.expanded').removeClass('expanded');

//now let's expand this one.
$(this).parent('div').toggleClass('expanded');
$(this).next('div').slideDown();
}

});

Demo jsfiddle here

关于jquery - 使用 JQuery 展开折叠 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39854234/

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