在用户使用JQuery点击页面上的单独元素后,如何显示元素?
我创建了一个包含以下HTML的侧边菜单:
<li id="navicon">
<a href="" class="nav">
<img src ="/assets/navicon-round.svg/" />
</a>
</li>
<ul class="side-categories">
<li><a href="">CSS</li></a>
<li><a href="">HTML</li></a>
<li><a href="">JS/Jquery</li></a>
<li><a href="">Miscellaneous</li></a>
</ul>
我将.side-categories
设置为display:none
现在,我希望能够在点击.side-categories
#nav
我刚开始学习JQuery,这就是我想出的。然而,它没有用。我希望有人可以告诉我这里做错了什么。
$( document ).ready(function() {
$('#nav').on('click')event(function()) {
event(showNavBar);
$('.side-categories').show();
});
});
答案 0 :(得分:3)
一个好方法是使用类隐藏侧边栏菜单。
在你的CSS中:
.hide {
display: none;
}
在你的HTML中:(如果你想要,可以删除<a>
,因为jQuery可以检测到任何元素的点击)
<li id="navicon">
<img class="nav" src ="/assets/navicon-round.svg/" />
</li>
<ul class="side-categories hide">
<li><a href="#">CSS</a></li>
<!-- More menu items -->
</ul>
然后在你的jQuery中:
$(function() {
$('.nav').on('click', function(e) {
e.preventDefault();
$('.side-categories').toggleClass('hide');
});
});
这是一种非常简单的方法,允许用户通过单击相同的按钮再次隐藏侧面菜单 - 这通常是预期的行为。
另一个好处是你可以保持你的功能和风格分开。
答案 1 :(得分:1)
<强> Working fiddle 强>
你应该替换:
event(showNavBar);
通过:
e.preventDefault();
完整代码:
$(function() {
$('.nav').on('click',function(e) {
e.preventDefault();
$('.side-categories').show();
});
});
答案 2 :(得分:1)
这是HTML代码:
$(function() {
$('.nav').on('click',function(e) {
e.preventDefault();
$(".side-categories").slideToggle();
});
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<li id="navicon">
<a href="" class="nav">
<img style="height:40px;position:absolute;" src ="data:image/gif;base64,R0lGODlhZABkAIABAIeHh////yH/C1hNUCBEYXRhWE1QPD94cGFja2V0IGJlZ2luPSLvu78iIGlkPSJXNU0wTXBDZWhpSHpyZVN6TlRj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BwL++vby7urm4t7a1tLOysbCvrq2sq6qpqKempaSjoqGgn56dnJuamZiXlpWUk5KRkI+OjYyLiomIh4aFhIOCgYB/fn18e3p5eHd2dXRzcnFwb25tbGtqaWhnZmVkY2JhYF9eXVxbWllYV1ZVVFNSUVBPTk1MS0pJSEdGRURDQkFAPz49PDs6OTg3NjU0MzIxMC8uLSwrKikoJyYlJCMiISAfHh0cGxoZGBcWFRQTEhEQDw4NDAsKCQgHBgUEAwIBAAAh+QQBAAABACwAAAAAZABkAAACtIyPqcvtD6OctNqLs968+w+G4kiW5omm6sq27gvH8kzX9o3n+s73/g8MCofEovGITCqXzKbzCY1eANSq9YrNardZGvcLDmO94rJ5PDurz+S1+9t+y9Gyuf0qzev3/L7/DxgoOFh3ZxhnKIeY6LbIqOb4aBYpKUZZCUaoucnZ6fkJGio6eoDZmGYKiZo6ucpq6fqaGSvLdVlrRaq7y9vr+wscLDxMXGx8jJysvMzc7PwMHU1aAAA7" />
</a>
</li>
<ul class="side-categories" style="display:none">
<li><a href="">CSS</li></a>
<li><a href="">HTML</li></a>
<li><a href="">JS/Jquery</li></a>
<li><a href="">Miscellaneous</li></a>
</ul>
&#13;
答案 3 :(得分:-1)
<强> HTML 强>
<li id="navicon">
<a href="" class="nav">
<img src ="/assets/navicon-round.svg/" />
</a>
</li>
<ul class="side-categories" style="display:none">
<li><a href="">CSS</li></a>
<li><a href="">HTML</li></a>
<li><a href="">JS/Jquery</li></a>
<li><a href="">Miscellaneous</li></a>
</ul>
替换您的JQuery
$(function() {
$('#navicon').click(function() { // Change # instead of .
$(".side-categories").show();
});
});
或强>
$(function() {
$('#navicon').on('click',function() {
$(".side-categories").show();
});
});
希望它有效.....