Home  >  Article  >  Web Front-end  >  How to query the first child node with jquery

How to query the first child node with jquery

藏色散人
藏色散人Original
2021-11-16 10:53:053630browse

Jquery method to query the first child node: 1. Obtain all child nodes through "$("#test").children();"; 2. Through "$.children(':first' )" to get the first child node.

How to query the first child node with jquery

The operating environment of this article: Windows 7 system, jquery version 3.2.1, DELL G3 computer

How to query the first child node with jquery?

jquery gets the first child node, js jquery gets the element (parent node, child node, sibling node)

1, js gets the element (parent node, child node, sibling node)

var test = document.getElementById("test");
var parent = test.parentNode; // 父节点
var chils = test.childNodes; // 全部子节点
var first = test.firstChild; // 第一个子节点
var last = test.lastChile; // 最后一个子节点
var previous = test.previousSbiling; // 上一个兄弟节点
var next = test.nextSbiling; // 下一个兄弟节点

2, jquery gets elements (parent node, child node, sibling node)

$("#test1").parent(); // 父节点
$("#test1").parents(); // 全部父节点
$("#test1").parents(".mui-content");
$("#test").children(); // 全部子节点
$("#test").children("#test1");
$("#test").contents(); // 返回#test里面的所有内容,包括节点和文本
$("#test").contents("#test1");
$("#test1").prev(); // 上一个兄弟节点
$("#test1").prevAll(); // 之前所有兄弟节点
$("#test1").next(); // 下一个兄弟节点
$("#test1").nextAll(); // 之后所有兄弟节点
$("#test1").siblings(); // 所有兄弟节点
$("#test1").siblings("#test2");
$("#test").find("#test1");

3, element filtering

// 以下方法都返回一个新的jQuery对象,他们包含筛选到的元素
$("ul li").eq(1); // 选取ul li中匹配的索引顺序为1的元素(也就是第2个li元素)
$("ul li").first(); // 选取ul li中匹配的第一个元素
$("ul li").last(); // 选取ul li中匹配的最后一个元素
$("ul li").slice(1, 4); // 选取第2 ~ 4个元素
$("ul li").filter(":even"); // 选取ul li中所有奇数顺序的元素

Recommended learning: "jquery video tutorial

The above is the detailed content of How to query the first child node with jquery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn