getElementsByClassName 与 jquery

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

getElementsByClassName vs. jquery

javascriptjquery

提问by Alex

If my original function was:

如果我原来的功能是:

document.getElementsByClassName('blah')[9].innerHTML = 'blah';

...how would I change that so I get that same item in jquery? I have this, but when I put '[9]' at the end it doesnt work:

...我将如何更改它以便在 jquery 中获得相同的项目?我有这个,但是当我把 '[9]' 放在最后时它不起作用:

$(data).find('.blah')[9].html();

It I leave the [9] out, it only gets the first item whose class name is 'blah', and I want it to get the 10th item.

如果我将 [9] 排除在外,它只会获取类名称为“blah”的第一个项目,而我希望它获取第 10 个项目。

回答by T.J. Crowder

The equivalent of

相当于

document.getElementsByClassName('blah')[9].innerHTML = 'blah';

is to use the :eqpseudo-selector:

是使用:eq伪选择器

$(".blah:eq(9)").html('blah');

or the eqfunction:

eq功能

$(".blah").eq(9).html('blah');

(...and then the htmlfunctionto set the inner HTML.)

(...然后是设置内部 HTML的html函数。)

回答by Jai

See what you are looking for is :eq():

看看你要找的是:eq()

$('.blah').eq(9).html('blah');

because :eq()is 0indexed,so :eq(9)will find the item at 10th index.

因为:eq()0编入索引,所以:eq(9)会在第 10 个索引处找到该项目。

.eq() jQuery doc

.eq() jQuery 文档

There is :nth-child()function too:

也有:nth-child()功能:

$('.blah:nth-child(10)').html('blah');

because :nth-child()is 1indexed so you have to give place 10th position there.

因为:nth-child()1索引所以你必须在那里让位第 10 位。

:nth-child() jQuery doc

:nth-child() jQuery 文档

from the docs:

从文档:

Because jQuery's implementation of :nth- selectors is strictly derived from the CSS specification, the value of n is "1-indexed", meaning that the counting starts at 1. For other selector expressions such as :eq() or :even jQuery follows JavaScript's "0-indexed" counting. Given a single containing two

  • s, $('li:nth-child(1)') selects the first
  • while $('li:eq(1)') selects the second.

  • 因为 jQuery 对 :nth- 选择器的实现严格源自 CSS 规范,所以 n 的值是“1-indexed”,这意味着从 1 开始计数。对于其他选择器表达式,例如 :eq() 或 :even jQuery 紧随其后JavaScript 的“0 索引”计数。给定一个包含两个

  • s, $('li:nth-child(1)') 选择第一个
  • 而 $('li:eq(1)') 选择第二个。

  • 回答by Ankush Jain

    try the following

    尝试以下

    $('.blah').eq(9).html('blah');
    

    回答by Ahmad Amin

    Another answer could be:

    另一个答案可能是:

    $($(data).find('.blah')[9]).html();
    

    When you use [9] it returns a DOM object which doesn't know what function html() is but without [9] it returns a jQuery Object which the html() function is apart of.

    当您使用 [9] 时,它返回一个 DOM 对象,该对象不知道 html() 是什么函数,但没有 [9] 时,它返回一个 html() 函数所包含的 jQuery 对象。

    回答by Tamil Selvan C

    Try this

    尝试这个

    $(".blah:eq(9)").html('blah');
    

    回答by Rohan210

    $('.blah')[9].innerHTML="BLAH";
    

    This should solve your problem

    这应该可以解决您的问题

    回答by Suresh Atta

    Try this

    尝试这个

    $('.blah').eq(9).html('blah');
    

    回答by supershnee

    You should also just be able to use jQuery's get() method:

    您还应该能够使用 jQuery 的 get() 方法:

    $('.blah').get(9)
    

    jQuery objects also function as indexed arrays as returned elements, so this should also work:

    jQuery 对象也可以作为索引数组作为返回元素,所以这也应该有效:

    $('.blah')[9]