admin管理员组文章数量:1410737
I am attempting to build a Jquery/CSS drop down menu and things have been going pretty good so far. I'm pretty new to JQuery and I get it most of the time - however, I cannot figure out what I am doing wrong here.
I am attempting to change the class of one of the drop-down tabs when it is clicked, but it doesn't appear to be working as expected.
You can see the code I have so far over here: /
I have the class "selected" looking how I want the button to look when a button is clicked on, but I cannot seem to make it so the "selected" class is applied on a click event. In theory, this:
<li id="menuProducts"><a href="#" class="hasmore">Products</a>
<div id="ProductsMenu">
<ul>
<li><a href="#">Submenu Item</a></li>
<li><a href="#">Submenu Item</a></li>
</ul>
</div>
</li>
Should change this this on a click event:
<li id="menuProducts"><a href="#" class="selected">Products</a>
<div id="ProductsMenu">
<ul>
<li><a href="#">Submenu Item</a></li>
<li><a href="#">Submenu Item</a></li>
</ul>
</div>
</li>
And this is my current jQuery (which doesn't work):
jQuery(document).ready(function () {
$('#menuProducts a:first').on('click',
function () {
$("a:first").removeClass("hasmore");
$("a:first").addClass("selected");
menuSubCloseAll.call(this, 'menuProducts');
$('#menuProducts').find('li').slideToggle(200);
});
});
I've tried rewriting this code in many,many different ways and I'm running out of ideas on what I could be doing wrong.
Anyone out there have any pointers on what I could do to fix this?
Thank you in advance!!
I am attempting to build a Jquery/CSS drop down menu and things have been going pretty good so far. I'm pretty new to JQuery and I get it most of the time - however, I cannot figure out what I am doing wrong here.
I am attempting to change the class of one of the drop-down tabs when it is clicked, but it doesn't appear to be working as expected.
You can see the code I have so far over here: http://jsfiddle/utdream/NZJXq/
I have the class "selected" looking how I want the button to look when a button is clicked on, but I cannot seem to make it so the "selected" class is applied on a click event. In theory, this:
<li id="menuProducts"><a href="#" class="hasmore">Products</a>
<div id="ProductsMenu">
<ul>
<li><a href="#">Submenu Item</a></li>
<li><a href="#">Submenu Item</a></li>
</ul>
</div>
</li>
Should change this this on a click event:
<li id="menuProducts"><a href="#" class="selected">Products</a>
<div id="ProductsMenu">
<ul>
<li><a href="#">Submenu Item</a></li>
<li><a href="#">Submenu Item</a></li>
</ul>
</div>
</li>
And this is my current jQuery (which doesn't work):
jQuery(document).ready(function () {
$('#menuProducts a:first').on('click',
function () {
$("a:first").removeClass("hasmore");
$("a:first").addClass("selected");
menuSubCloseAll.call(this, 'menuProducts');
$('#menuProducts').find('li').slideToggle(200);
});
});
I've tried rewriting this code in many,many different ways and I'm running out of ideas on what I could be doing wrong.
Anyone out there have any pointers on what I could do to fix this?
Thank you in advance!!
Share Improve this question edited May 1, 2013 at 0:24 utdream asked May 1, 2013 at 0:16 utdreamutdream 5831 gold badge3 silver badges16 bronze badges 3-
2
change
$("a:first")
with$(this)
. – Omar Commented May 1, 2013 at 0:21 - lol ::facepalm:: thanks Omar. – utdream Commented May 1, 2013 at 0:26
- a ment is faster than an answer and safer ;) wele buddy – Omar Commented May 1, 2013 at 0:27
2 Answers
Reset to default 2Do it like this:
jQuery(document).ready(function () {
$('#menuProducts a:first').on('click',
function () {
$(this).removeClass("hasmore");
$(this).addClass("selected");
menuSubCloseAll.call(this, 'menuProducts');
$('#menuProducts').find('li').slideToggle(200);
});
});
You were actually changing the class of the first <a>
in your page, not the <a>
you clicked, which you can access directly in the handler with $(this)
.
Did you try use "this" inside the call??
$('#menuProducts a:first').on('click',
function () {
$(this).removeClass("hasmore");
$(this).addClass("selected");
menuSubCloseAll.call(this, 'menuProducts');
$('#menuProducts').find('li').slideToggle(200);
});
It worked for me
本文标签: javascriptJQuery addClass() on Click EventStack Overflow
版权声明:本文标题:javascript - JQuery addClass() on Click Event - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745030971a2638493.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论