javascript Jquery .addClass 和 .removeClass 不工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24912687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery .addClass and .removeClass Not Working?
提问by mesut
I wrote a code that clicking an item displays a context menu like div to enabling user to cut and paste a list item, I added disabled class to buttons in div that are related to paste operation. but when something selected I want to remove the class 'disable' to enable paste buttons. the code is as below:
我写了一个代码,点击一个项目会显示一个上下文菜单,比如 div 以允许用户剪切和粘贴列表项,我向 div 中与粘贴操作相关的按钮添加了禁用类。but when something selected I want to remove the class 'disable' to enable paste buttons. 代码如下:
html:
html:
<div id="contextMenu" class="text-center" style="display: none; z-index: 1001; width: 242px;box-shadow:#D3D3D3 0px 0px 10px 4px; background-color: #FDFFC7; padding: 15px;">
<a class="btn btn-primary" style="margin: 3px 6px 3px 0px;" href="#" id="cut"><i class="fa fa-cut fa-lg"></i> Kes</a>
<div class="btn-group" style="margin: 3px 0px 3px 6px;">
<button type="button" class="btn btn-success disabled" id="InsertInto" title="Kesti?ini kategoriyi se?ti?iniz kategorinin i?ine yap??t?r?r."><i class="fa fa-paste fa-lg"></i> Yap??t?r</button>
<button type="button" class="btn btn-success dropdown-toggle disabled" data-toggle="dropdown"><span class="caret"></span></button>
<ul class="dropdown-menu" role="menu">
<li><a href="#" title="Se?ti?iniz kategorinin üstüne ekler" id="AddAbove"><i class="fa fa-level-up fa-lg"></i> üstüne Ekle</a></li>
<li><a href="#" title="Se?ti?iniz kategorinin Alt?na ekler" id="AddBelow"><i class="fa fa-level-down fa-lg"></i> Alt?na Ekle</a></li>
</ul>
</div>
JS:
JS:
$(document).on('click', '.ContextMenu', function () {
event.preventDefault();
var anySelected = $('.selected').length;
if (anySelected > 0) {
$("button", "#contextManu").removeClass('disabled');
}
var widgetPosition = $('#contextMenu').parent().offset();
$('#contextMenu').css('position', 'absolute');
var currentPosition = $(this).offset();
var contextLeft = (currentPosition.left) - $('#contextMenu').width() - widgetPosition.left - 10;
var contextTop = (currentPosition.top + 80) - widgetPosition.top;
$('#contextMenu').css('left', contextLeft);
$('#contextMenu').css('top', contextTop);
$('#contextMenu').slideDown(300);
$selectedListItem = $(this).parents('div.categoryItemContainer').parent();
});
I Trace code using google chrome browser, $("button", "#contextManu") founds two button in the contextMenu div but not changes it.
我使用 google chrome 浏览器跟踪代码,$("button", "#contextManu") 在 contextMenu div 中找到了两个按钮,但没有更改它。
回答by Code Maverick
You have a typo in your selector at this line:
您在这一行的选择器中有一个错字:
$("button", "#contextManu").removeClass('disabled');
#contextManu
needs to have the a
changed to an e
like so:
#contextManu
需要a
更改为e
这样的:
$("button", "#contextMenu").removeClass('disabled');
回答by Damon
You could also use toggleClass. I recently found that gem out myself.
你也可以使用toggleClass。我最近自己发现了那颗宝石。
$('button', '#contextMenu').toggleClass('disabled');
From the docs:Add or remove one or more classes from each element in the set of matched elements, depending on either the class's presence or the value of the switch argument.
来自文档:根据类的存在或 switch 参数的值,从匹配元素集中的每个元素中添加或删除一个或多个类。
So, you don't even need to add "disabled" to your element(s) to begin with.
因此,您甚至不需要在开始时向元素添加“禁用”。