简体   繁体   English

使用jQuery单击时显示/隐藏li图标

[英]Show/hide li icon when clicked on with jQuery

Building my first website, so please be patient with me. 建立我的第一个网站,所以请耐心等待。 I'm trying to hide/show list item icon when clicked on the particular li, I know how to go around this using CSS3, but I would like to do use jQuery. 当我单击特定的li时,我试图隐藏/显示列表项图标,我知道如何使用CSS3解决此问题,但我想使用jQuery。 Any tips? 有小费吗?

Here is my html code: 这是我的html代码:

<ul id="nav" class="grid_1 push_3"> 
    <li class="icon"><a href="#">Home</a></li>
    <li class="icon"><a href="#">Gallery</a></li>
    <li class="icon"><a href="#">Recipies</a></li>
    <li class="icon"><a href="#">Contact</a></li>
</ul>

and my CSS: 和我的CSS:

ul {
    list-style-type: none;
    padding: 0;
    margin-top:2%;
}

ul li{
    font-family: 'cheveuxdangemedium', Georgia, Serif;
    font-size: 1em;
}

ul li a {
    display: block;
    text-decoration: none;
    color: #bec4b8;
    text-align: left;
    text-transform: uppercase;
    margin-left: -0.4em;
}

li.icon {
    list-style-image: url("../images/greanleaf.png");
    width: 100%;
    margin-bottom: -0.1em;
}

It appears as though you're asking how to toggle the icon class. 似乎您在询问如何切换icon类。

$('#nav li').click(function() {
    $(this).addClass('icon').siblings('li').removeClass('icon');
});

You'll need to implement this in a <script> tag after your HTML markup, or you'll need to wrap it in a document.ready function to run it when the markup has been rendered. HTML标记后,您需要在<script>标记中实现此功能,或者将其包装在document.ready函数中,以在标记呈现后运行它。

In the future, please do make an effort before asking and show us what's not working. 将来,请在询问并向我们展示哪些行不通之前,请先做出努力。 The jQuery docs are very easy to follow. jQuery文档非常容易遵循。

$('#nav li').on('click', function() {
    $(this).toggleClass('icon')
});

You can do this with native HTML5. 您可以使用原生HTML5做到这一点。 This isn't yet supported in all major browsers, but there are polyfills that can implant that functionality into browsers that don't yet support it. 并非所有主流浏览器都支持此功能,但是有一些polyfill可以将该功能植入尚不支持该功能的浏览器中。

<details>
  <summary>Click here</summary>
  <p>Now this shows.</p>
</details>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM