[英]Pure JS equivalent of Jquery eq()
What is the pure equivalent of jquery's eq()
. 什么是jquery的
eq()
的纯粹等价物。 For example, how may I achieve 例如,我该如何实现
$(".class1.class2").eq(0).text(1254);
in pure javascript? 在纯JavaScript中?
To get the element index in the array you can use []
in javascript. 要获取数组中的元素索引,可以在javascript中使用
[]
。 So to reproduce your code you can use this: 因此,要重现您的代码,您可以使用:
document.querySelectorAll('.class1.class2')[0].textContent = 1254;
or 要么
document.querySelectorAll('.class1.class2')[0].innerHTML = 1254;
1254
is a number, if you have a string you should use = 'string';
1254
是一个数字,如果你有一个字符串,你应该使用= 'string';
with quotes. .querySelector()
insteal of .querySelectorAll()
. .querySelector()
的.querySelectorAll()
。 More reading: 更多阅读:
MDN: textContent
MDN: textContent
MDN: innerHTMLMDN: innerHTML
MDN: querySelectorAllMDN: querySelectorAll
Here's one way to achieve it. 这是实现它的一种方法。 Tested working!
经过测试工作! It splits up the string you want to select into the parts before the
:eq
and after the :eq
, and then runs them separately. 它将您要选择的字符串拆分为
:eq
之前和之后的部分:eq
,然后单独运行它们。 It repeats until there's no more :eq
left. 它重复,直到不再有
:eq
离开。
var querySelectorAllWithEq = function(selector, document) {
var remainingSelector = selector;
var baseElement = document;
var firstEqIndex = remainingSelector.indexOf(':eq(');
while (firstEqIndex !== -1) {
var leftSelector = remainingSelector.substring(0, firstEqIndex);
var rightBracketIndex = remainingSelector.indexOf(')', firstEqIndex);
var eqNum = remainingSelector.substring(firstEqIndex + 4, rightBracketIndex);
eqNum = parseInt(eqNum, 10);
var selectedElements = baseElement.querySelectorAll(leftSelector);
if (eqNum >= selectedElements.length) {
return [];
}
baseElement = selectedElements[eqNum];
remainingSelector = remainingSelector.substring(rightBracketIndex + 1).trim();
// Note - for now we just ignore direct descendants:
// 'a:eq(0) > i' gets transformed into 'a:eq(0) i'; we could maybe use :scope
// to fix this later but support is iffy
if (remainingSelector.charAt(0) === '>') {
remainingSelector = remainingSelector.substring(1).trim();
}
firstEqIndex = remainingSelector.indexOf(':eq(');
}
if (remainingSelector !== '') {
return Array.from(baseElement.querySelectorAll(remainingSelector));
}
return [baseElement];
};
querySelectorAll
返回一个数组,因此您可以使用索引获取元素0
document.querySelectorAll(".class1.class2")[0].innerHTML = 1254
document.querySelectorAll(".class1.class2")[0].innerHTML = '1254';
Element.querySelectorAll Element.querySelectorAll
Summary 摘要
Returns a non-live NodeList of all elements descended from the element on which it is invoked that match the specified group of CSS selectors. 返回从调用它的元素的所有元素的非实时NodeList,该元素与指定的CSS选择器组匹配。
Syntax 句法
elementList = baseElement.querySelectorAll(selectors);
https://developer.mozilla.org/en-US/docs/Web/API/Element.querySelectorAll https://developer.mozilla.org/en-US/docs/Web/API/Element.querySelectorAll
Since you're only getting the first one, document.querySelector(".class1.class2")
will suffice. 由于您只获得第一个,因此
document.querySelector(".class1.class2")
就足够了。 It returns the element itself, and doesn't have to build an entire node list just to get the first. 它返回元素本身,并且不必为了获得第一个而构建整个节点列表。
However, if you want anything other than the first, then you will need querySelectorAll
. 但是,如果你想要除了第一个之外的任何东西,那么你将需要
querySelectorAll
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.