jquery toggleClass 不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9569065/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:59:41  来源:igfitidea点击:

jquery toggleClass not working

jquerytoggleclass

提问by Henry Ing-Simmons

For some reason the toggleClass function does not seem to be working.

由于某种原因,toggleClass 函数似乎不起作用。

I have a table row with a class "nodrag" - being used with tableDnD plugin:

我有一个带有“nodrag”类的表格行 - 与 tableDnD 插件一起使用:

<tr id="1" class="nodrag">
<!--Some tds and stuff-->       
</tr>

When I click a link I want to toggle the class on and off with the following code which is in the $(document).ready function:

当我单击一个链接时,我想使用 $(document).ready 函数中的以下代码打开和关闭类:

$("#reorder").click(function(event) {
    $("#1").toggleClass("nodrag");

    if ($(this).text()=='Reorder')
    {
        $(this).text("Done reordering");
    } else {
        $(this).text("Reorder");
    }

    event.preventDefault();
});

I know the click event fires because the text of the link changes. I also know the selection of the row works because if I do something else with it like - $("#1").text("test") - that works.

我知道点击事件是因为链接的文本发生变化而触发的。我也知道行的选择是有效的,因为如果我用它做其他事情,比如 - $("#1").text("test") - 那行得通。

It just seems to a problem with toggleClass. I've searched everywhere though and found nothing that helps me.

似乎toggleClass 有问题。我到处搜索,但没有发现任何帮助我的东西。

Thanks in advance for any help

在此先感谢您的帮助

采纳答案by Mohammed Swillam

this is a live demo that is working perfect for me

这是一个现场演示,非常适合我

http://jsfiddle.net/Yjqkn/

http://jsfiddle.net/Yjqkn/

Give it a try and let me know what is different between this code and yours.

试一试,让我知道这段代码和你的代码有什么不同。

P.S:you should always avoid naming your IDs with plain numbers, if you are inforced to do so, prefix it with any letter(s) before the number ex:TblRow1, txt2. but it's always recommended to name your controls with meaningful words ex: txtUsername, lnkDelete,etc.

PS:您应该始终避免使用纯数字命名您的 ID,如果您被迫这样做,请在数字前添加任何字母作为前缀,例如:TblRow1, txt2。但始终建议使用有意义的词命名您的控件,例如:txtUsername、lnkDelete等。

回答by Rizwan Mumtaz

It look like you entering stuff in you <tr>tag not in the <td>tag of the tr
I checked it work fine if you put the content inside <td>tag. See here http://jsfiddle.net/QjjGD/1/

看起来你在你的<tr>标签中输入的东西不是在 我检查的<td>标签中,tr
如果你把内容放在<td>标签中,它工作正常。见这里http://jsfiddle.net/QjjGD/1/