jQuery 确定匹配的类是否具有给定的 id
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2253457/
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 Determine if a matched class has a given id
提问by Nicholas Murray
What is jQuery has id equivalent of the following statement?
什么是 jQuery 具有等效于以下语句的 id?
$('#mydiv').hasClass('foo')
so you could give a class name and check that it contains a supplied id.
所以你可以给出一个类名并检查它是否包含一个提供的 id。
something like:
就像是:
$('.mydiv').hasId('foo')
回答by Sampson
You can bake that logic into the selector by combining multiple selectors. For instance, we could target all elements with a given id, that also have a particular class:
您可以通过组合多个选择器来将该逻辑烘焙到选择器中。例如,我们可以定位具有给定 id 的所有元素,这些元素也具有特定的类:
$("#foo.bar"); // Matches <div id="foo" class="bar">
This should look similar to something you'd write in CSS. Note that it won't apply to all #foo
elements (though there should only be one), and it won't apply to all .bar
elements (though there may be many). It will only reference elements that qualify on both attributes.
这应该类似于您在 CSS 中编写的内容。请注意,它不会适用于所有#foo
元素(尽管应该只有一个),也不会适用于所有.bar
元素(尽管可能有很多)。它只会引用符合这两个属性的元素。
jQuery also has a great .is
methodthat lets your determine whether an element has certain qualities. You can test a jQuery collection against a string selector, an HTML Element, or another jQuery object. In this case, we'll just check it against a string selector:
jQuery 也有一个很棒的.is
方法,可以让您确定元素是否具有某些特性。您可以针对字符串选择器、HTML 元素或其他 jQuery 对象测试 jQuery 集合。在这种情况下,我们将只根据字符串选择器检查它:
$(".bar:first").is("#foo"); // TRUE if first '.bar' in document is also '#foo'
回答by prodigitalson
I would probably use $('.mydiv').is('#foo');
That said if you know the Id why wouldnt you just apply it to the selector in the first place?
我可能会使用$('.mydiv').is('#foo');
That say 如果您知道 Id 为什么不首先将其应用于选择器?
回答by ewwink
update: sorry misunderstood the question, removed .has()
answer.
更新:抱歉误解了这个问题,删除了.has()
答案。
another alternative way, create .hasId()
plugin
另一种替代方法,创建.hasId()
插件
// the plugin
$.fn.hasId = function(id) {
return this.attr('id') == id;
};
// select first class
$('.mydiv').hasId('foo') ?
console.log('yes') : console.log('no');
// select second class
// $('.mydiv').eq(1).hasId('foo')
// or
$('.mydiv:eq(1)').hasId('foo') ?
console.log('yes') : console.log('no');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="mydiv" id="foo"></div>
<div class="mydiv"></div>
回答by Nima Foladi
Let's say that you're iterating through some DOM objects and you wanna find and catch an element with a certain ID
假设您正在遍历一些 DOM 对象,并且想要查找并捕获具有特定 ID 的元素
<div id="myDiv">
<div id="fo"><div>
<div id="bar"><div>
</div>
You can either write something like to find
你可以写一些类似的东西来找到
$('#myDiv').find('#bar')
Note that if you were to use a class selector, the find method will return all the matching elements.
请注意,如果您要使用类选择器,则 find 方法将返回所有匹配的元素。
or you could write an iterating function that will do more advanced work
或者你可以写一个迭代函数来做更高级的工作
<div id="myDiv">
<div id="fo"><div>
<div id="bar"><div>
<div id="fo1"><div>
<div id="bar1"><div>
<div id="fo2"><div>
<div id="bar2"><div>
</div>
$('#myDiv div').each(function() {
if($(this).attr('id') == 'bar1')
//do something with bar1
});
Same code could be easily modified for class selector.
可以为类选择器轻松修改相同的代码。
<div id="myDiv">
<div class="fo"><div>
<div class="bar"><div>
<div class="fo"><div>
<div class="bar"><div>
<div class="fo"><div>
<div class="bar"><div>
</div>
$('#myDiv div').each(function() {
if($(this).hasClass('bar'))
//do something with bar
});
I'm glad you solved your problem with index(), what ever works for you.I hope this will help others with the same problem. Cheers :)
我很高兴你用 index() 解决了你的问题,什么对你有用。我希望这会帮助其他有同样问题的人。干杯:)
回答by Pointy
$('#' + theMysteryId + '.someClass').each(function() { /* do stuff */ });
回答by Stella
Just to say I eventually solved this using index().
只是说我最终使用 index() 解决了这个问题。
NOTHING else seemed to work.
没有其他似乎工作。
So for sibling elements this is a good work around if you are first selecting by a common class and then want to modify something differently for each specific one.
因此,对于同级元素,如果您首先通过一个公共类进行选择,然后想要为每个特定的类进行不同的修改,那么这是一个很好的解决方法。
EDIT: for those who don't know (like me) index() gives an index value for each element that matches the selector, counting from 0, depending on their order in the DOM. As long as you know how many elements there are with class="foo" you don't need an id.
编辑:对于那些不知道的人(比如我) index() 为每个匹配选择器的元素提供一个索引值,从 0 开始计数,具体取决于它们在 DOM 中的顺序。只要您知道 class="foo" 有多少元素,您就不需要 id。
Obviously this won't always help, but someone might find it useful.
显然这并不总是有帮助,但有人可能会发现它很有用。
回答by Vi?t Anh
Check if element's ID is exist
检查元素的ID是否存在
if ($('#id').attr('id') == 'id')
{
//OK
}
回答by user3379167
You could also check if the id element is used by doing so:
您还可以通过这样做来检查是否使用了 id 元素:
if(typeof $(.div).attr('id') == undefined){
//element has no id
} else {
//element has id selector
}
I use this method for global dataTables and specific ordered dataTables
我将此方法用于全局数据表和特定的有序数据表