纯JS相当于Jquery eq() [英] Pure JS equivalent of Jquery eq()
本文介绍了纯JS相当于Jquery eq()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
什么是jquery的 eq()
的纯粹等价物。例如,我怎样才能实现
What is the pure equivalent of jquery's eq()
. For example, how may I achieve
$(".class1.class2").eq(0).text(1254);
推荐答案
要获取数组中的元素索引,可以在javascript中使用 []
。因此,要重现您的代码,您可以使用:
To get the element index in the array you can use []
in javascript. So to reproduce your code you can use this:
document.querySelectorAll('.class1.class2')[0].textContent = 1254;
或
document.querySelectorAll('.class1.class2')[0].innerHTML = 1254;
- 在你的例子中
1254
是一个数字,如果你有一个字符串,你应该使用带引号的='string';
。 - 如果你只是在寻找一个/第一个元素,你可以只使用
.querySelector()
.querySelectorAll()
。 - In your example
1254
is a number, if you have a string you should use= 'string';
with quotes. - If you are only looking for one/the first element you can use just
.querySelector()
insteal of.querySelectorAll()
.
Demo 这里
Demo here
更多阅读:
MDN: textContent
MDN: innerHTML
MDN: querySelectorAll
这篇关于纯JS相当于Jquery eq()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文