[英]convert hover to click toggle
i want to convert from hover to mouse click, here's some of the code我想从悬停转换为鼠标点击,这是一些代码
HTML HTML
<li class="has-dropdown">
<a href="#">Click Me</a>
<ul class="dropdown">
<li><a href="#">Web Design</a></li>
<li><a href="#">eCommerce</a></li>
<li><a href="#">Linux</a></li>
<li><a href="#">API</a></li>
</ul>
</li>
and here's the script but still in mouseover/mouseleave这是脚本,但仍在鼠标悬停/鼠标离开
var dropdown = function() {
$('.has-dropdown').mouseenter(function(){
var $this = $(this);
$this
.find('.dropdown')
.css('display', 'block')
.addClass('animated-fast fadeInUpMenu');
}).mouseleave(function(){
var $this = $(this);
$this
.find('.dropdown')
.css('display', 'none')
.removeClass('animated-fast fadeInUpMenu');
});
};
dropdown()
How can I change this from hover to click?如何将其从悬停更改为单击?
You can use the onclick="YourFunction()" and it will execute the function only when you click it.您可以使用 onclick="YourFunction()" 并且只有当您单击它时它才会执行该功能。
Not sure if this is what you want, but see this example不确定这是否是您想要的,但请参阅此示例
HTML HTML
<input type="button" value="Click me" onclick="showlist()" />
<div id="list" style="display:none;" >
<ul class="dropdown" type="hidden">
<li><a href="#">Web Design</a></li>
<li><a href="#">eCommerce</a></li>
<li><a href="#">Linux</a></li>
<li><a href="#">API</a></li>
</ul>
</div>
JS JS
function showlist() {
var x = document.getElementById("list");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
http://jsfiddle.net/5an2mo0g/4/ http://jsfiddle.net/5an2mo0g/4/
You could use .click
with .toggleClass
你可以使用
.click
与.toggleClass
$('#toggle').click(function() { $('.has-dropdown') .find('.dropdown') .css({ display: function(_, value) { return value === 'none' ? 'block' : 'none' } }) .toggleClass('animated-fast fadeInUpMenu') })
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <li class="has-dropdown"> <a id="toggle" href="#">Click Me</a> <ul class="dropdown"> <li><a href="#">Web Design</a></li> <li><a href="#">eCommerce</a></li> <li><a href="#">Linux</a></li> <li><a href="#">API</a></li> </ul> </li>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.