jQuery 检查 ID 是否相等
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9751042/
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
Check if ID equals
提问by santa
I need to remove a class from all table cells when an element's parent has a particular ID.
当元素的父元素具有特定 ID 时,我需要从所有表格单元格中删除一个类。
$(".closethis").click(function(){
var $this = $(this).parent().parent();
if ($this.attr("id") == "mainArea") {
$("#myTbl").removeClass("myClass");
}
});
I do need to check for ID because this is part of a function and if ID is not equal to this value it is probably for another case.
我确实需要检查 ID,因为这是函数的一部分,如果 ID 不等于此值,则可能是另一种情况。
Looks right but it does not seem to work. Am I missing something?
看起来不错,但似乎不起作用。我错过了什么吗?
回答by jgradim
Try using the is
function present in jQuery.
尝试使用is
jQuery 中的函数。
if ($this.is('#mainArea')) { ... }
回答by whiteatom
Hmm.. Ok.. $var looks like PHP.. i'd just call it was it is. Also, you have #. in your myTbl which means id and class.... so it depends on how you are identifying the cells.. but assuming the cells are td's inside a table ID'd "myTbl", try this
嗯.. 好吧.. $var 看起来像 PHP.. 编号只是调用它是。另外,你有#。在您的 myTbl 中,这意味着 id 和 class .... 所以这取决于您如何识别单元格 .. 但假设单元格在表 ID“myTbl”中的 td,试试这个
$(".closethis").click(function(){
var checkDiv = $(this).parent().parent();
if (checkDiv.attr("id") == "mainArea") {
$("#myTbl").find('td').removeClass("myClass");
}
});
If you can't make that work, give us the ID's or class's of each element and we can give you the correct code.
如果您无法完成这项工作,请将每个元素的 ID 或类提供给我们,我们可以为您提供正确的代码。