我创建了一个带有标签和“下箭头”图标的下拉按钮。当下拉菜单打开时,“向下插入”图标应向上旋转,这是可行的。
但是,如果我单击按钮标题或“下箭头”图标,则此事件不起作用。
$(document).ready(function() {
$('.dropdown').click(function(e) {
$($(e.target).find('.down-caret').toggleClass('open-caret'));
e.stopPropagation();
$(document).click(function() {
$('.dropdown-menu').removeClass('open');
$('.down-caret').removeClass('open-caret');
});
});
});
a {
text-decoration: none;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown .drop {
background-color: #3498db;
color: #fff;
padding: 1rem;
border-radius: 6px;
position: relative;
display: inline-block;
}
.down-caret {
width: 0;
height: 0;
border-style: solid;
border-width: 5px 5px 0 5px;
border-color: #ffffff transparent transparent transparent;
display: inline-block;
margin-left: 6px;
top: -3px;
position: relative;
transform: rotate(0deg);
transition: all 0.25s ease-in;
}
.open-caret {
transform: rotate(180deg);
transition: all 0.25s ease-out;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li class="dropdown">
<a class="drop" href="#">
<span>Dropdown</span>
<i class="down-caret"></i>
</a>
</li>
我需要在我的代码中存在span&i标签。
这是我的完整代码jsFiddle
答案 0 :(得分:6)
只需定位锚元素并稍微更改您的JS。
$(document).ready(function(){
$('a.drop').click(function(e){
e.preventDefault();
$('.down-caret',this).toggleClass('open-caret');
e.stopPropagation();
$(document).click(function(){
$('.dropdown-menu').removeClass('open');
$('.down-caret').removeClass('open-caret');
});
});
});
a {
text-decoration: none;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown .drop {
background-color: #3498db;
color: #fff;
padding: 1rem;
border-radius: 6px;
position: relative;
display: inline-block;
}
.down-caret {
width: 0;
height: 0;
border-style: solid;
border-width: 5px 5px 0 5px;
border-color: #ffffff transparent transparent transparent;
display: inline-block;
margin-left: 6px;
top: -3px;
position: relative;
transform: rotate(0deg);
transition: all 0.25s ease-in;
}
.open-caret {
transform: rotate(180deg);
transition: all 0.25s ease-out;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li class="dropdown">
<a class="drop" href="#">
<span>Dropdown</span>
<i class="down-caret"></i>
</a>
</li>
答案 1 :(得分:3)
原因是,您正在使用当前单击的元素$(e.target)
在类down-caret
中查找元素。
如果单击icon
或text
,则该指定类中没有元素。
您只需要将$(e.target)
更改为$('.dropdown')
就可以了。
$(document).ready(function(){
$('.dropdown').click(function(e){
$('.dropdown').find('.down-caret').toggleClass('open-caret');
});
});
a {
text-decoration: none;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown .drop {
background-color: #3498db;
color: #fff;
padding: 1rem;
border-radius: 6px;
position: relative;
display: inline-block;
}
.down-caret {
width: 0;
height: 0;
border-style: solid;
border-width: 5px 5px 0 5px;
border-color: #ffffff transparent transparent transparent;
display: inline-block;
margin-left: 6px;
top: -3px;
position: relative;
transform: rotate(0deg);
transition: all 0.25s ease-in;
}
.open-caret {
transform: rotate(180deg);
transition: all 0.25s ease-out;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<li class="dropdown">
<a class="drop" href="#">
<span>Dropdown</span>
<i class="down-caret"></i>
</a>
</li>
这是fiddle
答案 2 :(得分:1)
答案 3 :(得分:1)
为什么不使用目标来查找“下箭头”图标,为什么不直接使用“下图标”呢?
将x1_sub = ['service', 'claim', 'health', 'fund', 'premium', 'nice', 'process']
x2 = x1[x1['word'].isin(x1_sub)]
print (x2)
word score
0 service 1
5 claim 2
15 health 5
17 fund 4
22 premium 1
26 nice 3
更改为$(e.target).find('.down-caret')
答案 4 :(得分:0)
我检查了您的小提琴代码,并删除了<span>
及其正常工作。
我还更新了您的小提琴JSFiddle