跳至主要内容

类: abstract Cheerio<T>

Cheerio 类是该库的核心类。它封装了一组元素,并提供了一个 API 来遍历、修改和与该组元素进行交互。

加载一个文档将返回绑定到文档根元素的 Cheerio 类。当查询文档时(调用 $('selector'))将实例化该类。

示例

<ul id="fruits">
<li class="apple">Apple</li>
<li class="orange">Orange</li>
<li class="pear">Pear</li>
</ul>

扩展

类型参数

T

实现

  • ArrayLike<T>

可索引

[index: number]: T

属性

addClass()

addClass<T, R>(this, value?): R

将类(es)添加到所有匹配的元素。还接受一个 function

类型参数

T extends AnyNode

R extends ArrayLike<T>

参数

this: R

value?: string | (this, i, className) => undefined | string

新类的名称。

返回值

R

实例本身。

示例

$('.pear').addClass('fruit').html();
//=> <li class="pear fruit">Pear</li>

$('.apple').addClass('fruit red').html();
//=> <li class="apple fruit red">Apple</li>

参见

https://api.jqueryjs.cn/addClass/

定义在

src/api/attributes.ts:933


attr()

attr(this, name)

attr<T>(this, name): string | undefined

用于获取属性的方法。仅获取匹配集中第一个元素的属性值。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

name: string

属性的名称。

返回值

string | undefined

属性的值。

示例
$('ul').attr('id');
//=> fruits
参见

https://api.jqueryjs.cn/attr/

定义在

src/api/attributes.ts:114

attr(this)

attr<T>(this): Record<string, string> | undefined

用于获取匹配集中第一个元素的所有属性及其值的方法。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

返回值

Record<string, string> | undefined

属性的值。

示例
$('ul').attr();
//=> { id: 'fruits' }
参见

https://api.jqueryjs.cn/attr/

定义在

src/api/attributes.ts:133

attr(this, name, value)

attr<T>(this, name, value?): Cheerio<T>

用于设置属性的方法。仅设置匹配集中第一个元素的属性值。如果将属性的值设置为 null,则会删除该属性。您也可以传递一个 mapfunction

类型参数

T extends AnyNode

参数

this: Cheerio<T>

name: string

属性的名称。

value?: null | string | (this, i, attrib) => null | string

属性的新值。

返回值

Cheerio<T>

实例本身。

示例
$('.apple').attr('id', 'favorite').html();
//=> <li class="apple" id="favorite">Apple</li>
参见

https://api.jqueryjs.cn/attr/

定义在

src/api/attributes.ts:154

attr(this, values)

attr<T>(this, values): Cheerio<T>

用于一次设置多个属性的方法。仅设置匹配集中第一个元素的属性值。如果将属性的值设置为 null,则会删除该属性。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

values: Record<string, null | string>

属性名称和值的映射。

返回值

Cheerio<T>

实例本身。

示例
$('.apple').attr({ id: 'favorite' }).html();
//=> <li class="apple" id="favorite">Apple</li>
参见

https://api.jqueryjs.cn/attr/

定义在

src/api/attributes.ts:179


data()

data(this, name)

data<T>(this, name): unknown | undefined

用于获取数据属性的方法,仅针对匹配集中第一个元素。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

name: string

数据属性的名称。

返回值

unknown | undefined

数据属性的值,如果属性不存在则为 undefined

示例
$('<div data-apple-color="red"></div>').data('apple-color');
//=> 'red'
参见

https://api.jqueryjs.cn/data/

定义在

src/api/attributes.ts:629

data(this)

data<T>(this): Record<string, unknown>

用于获取元素所有数据属性的方法,仅针对匹配集中第一个元素。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

返回

Record<string, unknown>

包含所有数据属性的映射。

示例
$('<div data-apple-color="red"></div>').data();
//=> { appleColor: 'red' }
参见

https://api.jqueryjs.cn/data/

定义于

src/api/attributes.ts:648

data(this, name, value)

data<T>(this, name, value): Cheerio<T>

设置数据属性的方法,仅作用于匹配集合中的第一个元素。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

name: string

数据属性的名称。

value: unknown

新值。

返回

Cheerio<T>

实例本身。

示例
const apple = $('.apple').data('kind', 'mac');

apple.data('kind');
//=> 'mac'
参见

https://api.jqueryjs.cn/data/

定义于

src/api/attributes.ts:670

data(this, values)

data<T>(this, values): Cheerio<T>

一次设置多个数据属性的方法,仅作用于匹配集合中的第一个元素。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

values: Record<string, unknown>

名称到值的映射。

返回

Cheerio<T>

实例本身。

示例
const apple = $('.apple').data({ kind: 'mac' });

apple.data('kind');
//=> 'mac'
参见

https://api.jqueryjs.cn/data/

定义于

src/api/attributes.ts:693


hasClass()

hasClass<T>(this, className): boolean

检查任何匹配的元素是否具有给定的className

类型参数

T extends AnyNode

参数

this: Cheerio<T>

className: string

类的名称。

返回

boolean

指示元素是否具有给定的className

示例

$('.pear').hasClass('pear');
//=> true

$('apple').hasClass('fruit');
//=> false

$('li').hasClass('pear');
//=> true

参见

https://api.jqueryjs.cn/hasClass/

定义于

src/api/attributes.ts:890


prop()

prop(this, name)

prop<T>(this, name): string | undefined

获取和设置属性的方法。获取匹配集合中第一个元素的属性值。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

name: "tagName" | "nodeName"

属性的名称。

返回

string | undefined

如果指定了value,则返回实例本身,否则返回属性的值。

示例
$('input[type="checkbox"]').prop('checked');
//=> false

$('input[type="checkbox"]').prop('checked', true).val();
//=> ok
参见

https://api.jqueryjs.cn/prop/

定义于

src/api/attributes.ts:293

prop(this, name)

prop<T>(this, name): string | null

类型参数

T extends AnyNode

参数

this: Cheerio<T>

name: "innerText" | "outerHTML" | "textContent" | "innerHTML"

返回

string | null

定义于

src/api/attributes.ts:297

prop(this, name)

prop<T>(this, name): StyleProp | undefined

获取解析后的 CSS 样式对象。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

name: "style"

属性的名称。

返回

StyleProp | undefined

样式对象,如果元素没有style属性,则为undefined

定义于

src/api/attributes.ts:308

prop(this, name)

prop<T>(this, name): string | undefined

解析支持元素的hrefsrc。需要设置baseURI选项,并且环境中包含全局URL对象。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

name: "href" | "src"

属性的名称。

返回

string | undefined

解析后的 URL,如果元素不支持,则为undefined

示例
$('<img src="image.png">').prop('src');
//=> 'https://example.com/image.png'
定义于

src/api/attributes.ts:326

prop(this, name)

prop<T, K>(this, name): Element[K]

获取元素的属性。

类型参数

T extends AnyNode

K extends keyof Element

参数

this: Cheerio<T>

name: K

属性的名称。

返回

Element[K]

属性的值。

定义于

src/api/attributes.ts:336

prop(this, name, value)

prop<T, K>(this, name, value): Cheerio<T>

设置元素的属性。

类型参数

T extends AnyNode

K extends keyof Element

参数

this: Cheerio<T>

name: K

属性的名称。

value: Element[K] | (this, i, prop) => undefined | null | string | number | Record<string, string> | TagSourceCodeLocation | Document | Element | CDATA | Text | Comment | ProcessingInstruction | ChildNode[] | object | Attribute[] | <T>(this, recursive?) => T

要设置属性的值。

返回

Cheerio<T>

实例本身。

定义于

src/api/attributes.ts:347

prop(this, map)

prop<T>(this, map): Cheerio<T>

设置元素的多个属性。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

map: Record<string, undefined | null | string | number | boolean | Record<string, string> | TagSourceCodeLocation | Document | Element | CDATA | Text | Comment | ProcessingInstruction | ChildNode[] | object | Attribute[] | <T>(this, recursive?) => T>

要设置的属性对象。

返回

Cheerio<T>

实例本身。

示例
$('input[type="checkbox"]').prop({
checked: true,
disabled: false,
});
定义于

src/api/attributes.ts:369

prop(this, name, value)

prop<T>(this, name, value): Cheerio<T>

设置元素的属性。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

name: string

属性的名称。

value: null | string | boolean | (this, i, prop) => string | boolean

要设置属性的值。

返回

Cheerio<T>

实例本身。

定义于

src/api/attributes.ts:380

prop(this, name)

prop<T>(this, name): string

获取元素的属性。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

name: string

属性的名称。

返回

字符串

属性的值。

定义于

src/api/attributes.ts:395


removeAttr()

removeAttr<T>(this, name): Cheerio<T>

通过名称移除属性的方法。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

name: string

属性的名称。

返回

Cheerio<T>

实例本身。

示例

$('.pear').removeAttr('class').html();
//=> <li>Pear</li>

$('.apple').attr('id', 'favorite');
$('.apple').removeAttr('id class').html();
//=> <li>Apple</li>

参见

https://api.jqueryjs.cn/removeAttr/

定义于

src/api/attributes.ts:854


removeClass()

removeClass<T, R>(this, name?): R

从选定的元素中删除一个或多个以空格分隔的类。如果没有定义 `className`,则将删除所有类。也接受一个 `function`。

类型参数

T extends AnyNode

R extends ArrayLike<T>

参数

this: R

name?: string | (this, i, className) => undefined | string

类的名称。如果未指定,则删除所有元素。

返回

R

实例本身。

示例

$('.pear').removeClass('pear').html();
//=> <li class="">Pear</li>

$('.apple').addClass('red').removeClass().html();
//=> <li class="">Apple</li>

参见

https://api.jqueryjs.cn/removeClass/

定义于

src/api/attributes.ts:1001


toggleClass()

toggleClass<T, R>(this, value?, stateVal?): R

根据类的存在或开关参数的值,向匹配的元素添加或删除类。也接受一个 `function`。

类型参数

T extends AnyNode

R extends ArrayLike<T>

参数

this: R

value?: string | (this, i, className, stateVal?) => string

类的名称。也可以是一个函数。

stateVal?: boolean

如果指定,则为类的状态。

返回

R

实例本身。

示例

$('.apple.green').toggleClass('fruit green red').html();
//=> <li class="apple fruit red">Apple</li>

$('.apple.green').toggleClass('fruit green red', true).html();
//=> <li class="apple green fruit red">Apple</li>

参见

https://api.jqueryjs.cn/toggleClass/

定义于

src/api/attributes.ts:1072


val()

val(this)

val<T>(this): string | undefined | string[]

获取 input、select 和 textarea 值的方法。注意:目前尚未添加对 `map` 和 `function` 的支持。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

返回

string | undefined | string[]

值。

示例
$('input[type="text"]').val();
//=> input_text
参见

https://api.jqueryjs.cn/val/

定义于

src/api/attributes.ts:743

val(this, value)

val<T>(this, value): Cheerio<T>

设置 input、select 和 textarea 值的方法。注意:目前尚未添加对 `map` 和 `function` 的支持。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

value: string | string[]

新值。

返回

Cheerio<T>

实例本身。

示例
$('input[type="text"]').val('test').html();
//=> <input type="text" value="test"/>
参见

https://api.jqueryjs.cn/val/

定义于

src/api/attributes.ts:762

CSS

css()

为每个匹配的元素设置多个 CSS 属性。

参数

属性的名称。

参数

新值。

参见

https://api.jqueryjs.cn/css/

css(this, names)

css<T>(this, names?): Record<string, string> | undefined

获取匹配元素集中第一个元素的样式属性值。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

names?: string[]

可选的感兴趣属性的名称。

返回

Record<string, string> | undefined

实例本身。

所有样式属性的映射。

参数

属性的名称。

参数

新值。

参见

https://api.jqueryjs.cn/css/

参见

https://api.jqueryjs.cn/css/

定义于

src/api/css.ts:14

css(this, name)

css<T>(this, name): string | undefined

获取匹配元素集中第一个元素的样式属性值。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

name: string

属性的名称。

返回值

string | undefined

实例本身。

给定名称的属性值。

参数

属性的名称。

参数

新值。

参见

https://api.jqueryjs.cn/css/

参见

https://api.jqueryjs.cn/css/

定义在

src/api/css.ts:27

css(this, prop, val)

css<T>(this, prop, val): Cheerio<T>

为每个匹配的元素设置一个 CSS 属性。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

prop: string

属性的名称。

val: string | (this, i, style) => undefined | string

新值。

返回值

Cheerio<T>

实例本身。

实例本身。

参数

属性的名称。

参数

新值。

参见

https://api.jqueryjs.cn/css/

参见

https://api.jqueryjs.cn/css/

定义在

src/api/css.ts:40

css(this, map)

css<T>(this, map): Cheerio<T>

为每个匹配的元素设置多个 CSS 属性。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

map: Record<string, string>

属性名称和值的映射。

返回值

Cheerio<T>

实例本身。

实例本身。

参数

属性的名称。

参数

新值。

参见

https://api.jqueryjs.cn/css/

参见

https://api.jqueryjs.cn/css/

定义在

src/api/css.ts:55

表单

serialize()

serialize<T>(this): string

将一组表单元素编码为字符串以提交。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

返回值

字符串

序列化后的表单。

示例

$('<form><input name="foo" value="bar" /></form>').serialize();
//=> 'foo=bar'

参见

https://api.jqueryjs.cn/serialize/

定义在

src/api/forms.ts:26


serializeArray()

serializeArray<T>(this): object[]

将一组表单元素编码为名称和值的数组。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

返回值

object[]

序列化后的表单。

示例

$('<form><input name="foo" value="bar" /></form>').serializeArray();
//=> [ { name: 'foo', value: 'bar' } ]

参见

https://api.jqueryjs.cn/serializeArray/

定义在

src/api/forms.ts:54

操作

after()

after<T>(this, ...elems): Cheerio<T>

在匹配元素集中的每个元素旁边插入内容。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

• ...elems: BasicAcceptedElems<AnyNode>[] | [(this, i, html) => BasicAcceptedElems<AnyNode>]

HTML 字符串、DOM 元素、DOM 元素数组或 Cheerio,要插入到匹配元素集中的每个元素之后。

返回值

Cheerio<T>

实例本身。

示例

$('.apple').after('<li class="plum">Plum</li>');
$.html();
//=> <ul id="fruits">
// <li class="apple">Apple</li>
// <li class="plum">Plum</li>
// <li class="orange">Orange</li>
// <li class="pear">Pear</li>
// </ul>

参见

https://api.jqueryjs.cn/after/

定义在

src/api/manipulation.ts:637


append()

append<T>(this, ...elems): Cheerio<T>

将内容作为选定元素中每个元素的最后一个子元素插入。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

• ...elems: BasicAcceptedElems<AnyNode>[] | [(this, i, html) => BasicAcceptedElems<AnyNode>]

返回值

Cheerio<T>

示例

$('ul').append('<li class="plum">Plum</li>');
$.html();
//=> <ul id="fruits">
// <li class="apple">Apple</li>
// <li class="orange">Orange</li>
// <li class="pear">Pear</li>
// <li class="plum">Plum</li>
// </ul>

参见

https://api.jqueryjs.cn/append/

定义在

src/api/manipulation.ts:270


appendTo()

appendTo<T>(this, target): Cheerio<T>

将匹配元素集中的每个元素插入到目标的末尾。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

target: BasicAcceptedElems<AnyNode>

要将元素追加到的元素。

返回值

Cheerio<T>

实例本身。

示例

$('<li class="plum">Plum</li>').appendTo('#fruits');
$.html();
//=> <ul id="fruits">
// <li class="apple">Apple</li>
// <li class="orange">Orange</li>
// <li class="pear">Pear</li>
// <li class="plum">Plum</li>
// </ul>

参见

https://api.jqueryjs.cn/appendTo/

定义在

src/api/manipulation.ts:207


before()

before<T>(this, ...elems): Cheerio<T>

在匹配元素集中的每个元素之前插入内容。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

• ...elems: BasicAcceptedElems<AnyNode>[] | [(this, i, html) => BasicAcceptedElems<AnyNode>]

HTML 字符串、DOM 元素、DOM 元素数组或 Cheerio,要插入到匹配元素集中的每个元素之前。

返回值

Cheerio<T>

实例本身。

示例

$('.apple').before('<li class="plum">Plum</li>');
$.html();
//=> <ul id="fruits">
// <li class="plum">Plum</li>
// <li class="apple">Apple</li>
// <li class="orange">Orange</li>
// <li class="pear">Pear</li>
// </ul>

参见

https://api.jqueryjs.cn/before/

定义在

src/api/manipulation.ts:746


clone()

clone<T>(this): Cheerio<T>

克隆 Cheerio 对象。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

返回值

Cheerio<T>

克隆后的对象。

示例

const moreFruit = $('#fruits').clone();

参见

https://api.jqueryjs.cn/clone/

定义在

src/api/manipulation.ts:1099


empty()

empty<T>(this): Cheerio<T>

从选择器中的每个元素中删除所有子节点。文本节点和注释节点保持原样。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

返回值

Cheerio<T>

实例本身。

示例

$('ul').empty();
$.html();
//=> <ul id="fruits"></ul>

参见

https://api.jqueryjs.cn/empty/

定义在

src/api/manipulation.ts:935


html()

html(this)

html<T>(this): string | null

从第一个选定元素获取 HTML 内容字符串。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

返回值

string | null

HTML 内容字符串。

示例
$('.orange').html();
//=> Orange

$('#fruits').html('<li class="mango">Mango</li>').html();
//=> <li class="mango">Mango</li>
参见

https://api.jqueryjs.cn/html/

定义在

src/api/manipulation.ts:963

html(this, str)

html<T>(this, str): Cheerio<T>

用指定的内容替换每个选定元素的内容。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

str: string | Cheerio<T>

用来替换选择器内容的内容。

返回值

Cheerio<T>

实例本身。

示例
$('.orange').html('<li class="mango">Mango</li>').html();
//=> <li class="mango">Mango</li>
参见

https://api.jqueryjs.cn/html/

定义在

src/api/manipulation.ts:979


insertAfter()

insertAfter<T>(this, target): Cheerio<T>

将匹配元素集中的每个元素插入到目标元素之后。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

target: BasicAcceptedElems<AnyNode>

要插入元素之后的元素。

返回值

Cheerio<T>

新插入元素集。

示例

$('<li class="plum">Plum</li>').insertAfter('.apple');
$.html();
//=> <ul id="fruits">
// <li class="apple">Apple</li>
// <li class="plum">Plum</li>
// <li class="orange">Orange</li>
// <li class="pear">Pear</li>
// </ul>

参见

https://api.jqueryjs.cn/insertAfter/

定义在

src/api/manipulation.ts:690


insertBefore()

insertBefore<T>(this, target): Cheerio<T>

将匹配元素集中的每个元素插入到目标元素之前。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

target: BasicAcceptedElems<AnyNode>

要插入元素之前的元素。

返回值

Cheerio<T>

新插入元素集。

示例

$('<li class="plum">Plum</li>').insertBefore('.apple');
$.html();
//=> <ul id="fruits">
// <li class="plum">Plum</li>
// <li class="apple">Apple</li>
// <li class="orange">Orange</li>
// <li class="pear">Pear</li>
// </ul>

参见

https://api.jqueryjs.cn/insertBefore/

定义在

src/api/manipulation.ts:799


prepend()

prepend<T>(this, ...elems): Cheerio<T>

将内容插入到每个选定元素的第一个子节点中。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

• ...elems: BasicAcceptedElems<AnyNode>[] | [(this, i, html) => BasicAcceptedElems<AnyNode>]

返回值

Cheerio<T>

示例

$('ul').prepend('<li class="plum">Plum</li>');
$.html();
//=> <ul id="fruits">
// <li class="plum">Plum</li>
// <li class="apple">Apple</li>
// <li class="orange">Orange</li>
// <li class="pear">Pear</li>
// </ul>

参见

https://api.jqueryjs.cn/prepend/

定义在

src/api/manipulation.ts:298


prependTo()

prependTo<T>(this, target): Cheerio<T>

将匹配元素集中的每个元素插入到目标元素的开头。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

target: BasicAcceptedElems<AnyNode>

要将元素预先插入到的元素。

返回值

Cheerio<T>

实例本身。

示例

$('<li class="plum">Plum</li>').prependTo('#fruits');
$.html();
//=> <ul id="fruits">
// <li class="plum">Plum</li>
// <li class="apple">Apple</li>
// <li class="orange">Orange</li>
// <li class="pear">Pear</li>
// </ul>

参见

https://api.jqueryjs.cn/prependTo/

定义在

src/api/manipulation.ts:240


remove()

remove<T>(this, selector?): Cheerio<T>

从 DOM 中删除匹配元素集及其所有子元素。selector 用于过滤要删除的匹配元素集。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

selector?: string

要删除的元素的可选选择器。

返回值

Cheerio<T>

实例本身。

示例

$('.pear').remove();
$.html();
//=> <ul id="fruits">
// <li class="apple">Apple</li>
// <li class="orange">Orange</li>
// </ul>

参见

https://api.jqueryjs.cn/remove/

定义在

src/api/manipulation.ts:851


replaceWith()

replaceWith<T>(this, content): Cheerio<T>

content替换匹配元素。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

content: AcceptedElems<AnyNode>

匹配元素的替换内容。

返回值

Cheerio<T>

实例本身。

示例

const plum = $('<li class="plum">Plum</li>');
$('.pear').replaceWith(plum);
$.html();
//=> <ul id="fruits">
// <li class="apple">Apple</li>
// <li class="orange">Orange</li>
// <li class="plum">Plum</li>
// </ul>

参见

https://api.jqueryjs.cn/replaceWith/

定义在

src/api/manipulation.ts:887


text()

text(this)

text<T>(this): string

获取匹配元素集中的每个元素的组合文本内容,包括其后代。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

返回值

字符串

集合的文本内容。

示例
$('.orange').text();
//=> Orange

$('ul').text();
//=> Apple
// Orange
// Pear
参见

https://api.jqueryjs.cn/text/

定义于

src/api/manipulation.ts:1037

text(this, str)

text<T>(this, str): Cheerio<T>

将匹配元素集中每个元素的内容设置为指定的文本。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

str: string | (this, i, text) => string

设置为每个匹配元素内容的文本。

返回值

Cheerio<T>

实例本身。

示例
$('.orange').text('Orange');
//=> <div class="orange">Orange</div>
参见

https://api.jqueryjs.cn/text/

定义于

src/api/manipulation.ts:1054


toString()

toString<T>(this): string

将集合转换为字符串。.html() 的别名。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

返回值

字符串

渲染后的文档。

定义于

src/api/manipulation.ts:1013


unwrap()

unwrap<T>(this, selector?): Cheerio<T>

.unwrap() 函数从 DOM 中移除匹配元素集的父元素,并将匹配元素留在其位置。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

selector?: string

要检查父元素的的选择器。如果元素的父元素不匹配选择器,则该元素不会被展开。

返回值

Cheerio<T>

实例本身,用于链式操作。

示例

const $ = cheerio.load(
'<div id=test>\n <div><p>Hello</p></div>\n <div><p>World</p></div>\n</div>',
);
$('#test p').unwrap();

//=> <div id=test>
// <p>Hello</p>
// <p>World</p>
// </div>
const $ = cheerio.load(
'<div id=test>\n <p>Hello</p>\n <b><p>World</p></b>\n</div>',
);
$('#test p').unwrap('b');

//=> <div id=test>
// <p>Hello</p>
// <p>World</p>
// </div>

参见

https://api.jqueryjs.cn/unwrap/

定义于

src/api/manipulation.ts:514


wrap()

wrap<T>(this, wrapper): Cheerio<T>

.wrap() 函数可以接受任何可以传递给 $() 工厂函数的字符串或对象来指定 DOM 结构。该结构可以嵌套多层,但应只包含一个最内层的元素。此结构的副本将包裹在匹配元素集中每个元素周围。此方法返回原始的元素集以用于链式操作。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

wrapper: AcceptedElems<AnyNode>

要包裹在选择中每个元素周围的 DOM 结构。

返回值

Cheerio<T>

示例

const redFruit = $('<div class="red-fruit"></div>');
$('.apple').wrap(redFruit);

//=> <ul id="fruits">
// <div class="red-fruit">
// <li class="apple">Apple</li>
// </div>
// <li class="orange">Orange</li>
// <li class="plum">Plum</li>
// </ul>

const healthy = $('<div class="healthy"></div>');
$('li').wrap(healthy);

//=> <ul id="fruits">
// <div class="healthy">
// <li class="apple">Apple</li>
// </div>
// <div class="healthy">
// <li class="orange">Orange</li>
// </div>
// <div class="healthy">
// <li class="plum">Plum</li>
// </div>
// </ul>

参见

https://api.jqueryjs.cn/wrap/

定义于

src/api/manipulation.ts:403


wrapAll()

wrapAll<T>(this, wrapper): Cheerio<T>

.wrapAll() 函数可以接受任何可以传递给 $() 函数的字符串或对象来指定 DOM 结构。该结构可以嵌套多层,但应只包含一个最内层的元素。该结构将作为单个组包裹在匹配元素集中所有元素周围。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

wrapper: AcceptedElems<T>

要包裹在选择中所有匹配元素周围的 DOM 结构。

返回值

Cheerio<T>

实例本身。

示例

const $ = cheerio.load(
'<div class="container"><div class="inner">First</div><div class="inner">Second</div></div>',
);
$('.inner').wrapAll("<div class='new'></div>");

//=> <div class="container">
// <div class='new'>
// <div class="inner">First</div>
// <div class="inner">Second</div>
// </div>
// </div>
const $ = cheerio.load(
'<span>Span 1</span><strong>Strong</strong><span>Span 2</span>',
);
const wrap = $('<div><p><em><b></b></em></p></div>');
$('span').wrapAll(wrap);

//=> <div>
// <p>
// <em>
// <b>
// <span>Span 1</span>
// <span>Span 2</span>
// </b>
// </em>
// </p>
// </div>
// <strong>Strong</strong>

参见

https://api.jqueryjs.cn/wrapAll/

定义于

src/api/manipulation.ts:577


wrapInner()

wrapInner<T>(this, wrapper): Cheerio<T>

.wrapInner() 函数可以接受任何可以传递给 $() 工厂函数的字符串或对象来指定 DOM 结构。该结构可以嵌套多层,但应只包含一个最内层的元素。该结构将包裹在匹配元素集中每个元素的内容周围。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

wrapper: AcceptedElems<AnyNode>

要包裹在选择中每个元素内容周围的 DOM 结构。

返回值

Cheerio<T>

实例本身,用于链式操作。

示例

const redFruit = $('<div class="red-fruit"></div>');
$('.apple').wrapInner(redFruit);

//=> <ul id="fruits">
// <li class="apple">
// <div class="red-fruit">Apple</div>
// </li>
// <li class="orange">Orange</li>
// <li class="pear">Pear</li>
// </ul>

const healthy = $('<div class="healthy"></div>');
$('li').wrapInner(healthy);

//=> <ul id="fruits">
// <li class="apple">
// <div class="healthy">Apple</div>
// </li>
// <li class="orange">
// <div class="healthy">Orange</div>
// </li>
// <li class="pear">
// <div class="healthy">Pear</div>
// </li>
// </ul>

参见

https://api.jqueryjs.cn/wrapInner/

定义于

src/api/manipulation.ts:466

其他

cheerio

cheerio: "[cheerio object]"

定义于

src/cheerio.ts:118


length

length: number = 0

实现

ArrayLike.length

定义于

src/cheerio.ts:40


options

options: InternalOptions

定义于

src/cheerio.ts:43


prevObject

prevObject: undefined | Cheerio<any>

定义于

src/cheerio.ts:77


splice()

splice: (start, deleteCount?) => any[](start, deleteCount, ...items) => any[]

从数组中删除元素,并在必要时插入新元素以代替它们,返回删除的元素。

参数

start: number

要开始删除元素的数组中基于零的索引。

deleteCount?: number

要删除的元素数量。

返回值

any[]

包含已删除元素的数组。

从数组中删除元素,并在必要时插入新元素以代替它们,返回删除的元素。

参数

start: number

要开始删除元素的数组中基于零的索引。

deleteCount: number

要删除的元素数量。

• ...items: any[]

要插入数组中以代替已删除元素的元素。

返回值

any[]

包含已删除元素的数组。

定义于

src/cheerio.ts:120


[iterator]()

[iterator](): Iterator<T, any, undefined>

返回值

Iterator<T, any, undefined>

定义于

website/node_modules/typescript/lib/lib.es2015.iterable.d.ts:49


extract()

extract<M, T>(this, map): ExtractedMap<M>

从文档中提取多个值,并将它们存储在一个对象中。

类型参数

M extends ExtractMap

T extends AnyNode

参数

this: Cheerio<T>

map: M

包含键值对的对象。键是将要创建的对象属性的名称,值是用于提取值的选择器。

返回

ExtractedMap<M>

包含提取值的 对象。

定义在

src/api/extract.ts:62


filterArray()

filterArray<T>(nodes, match, xmlMode?, root?): Element[] | T[]

类型参数

T

参数

nodes: T[]

match: AcceptedFilters<T>

xmlMode?: boolean

root?: Document

返回

Element[] | T[]

定义在

src/api/traversing.ts:787


foo()

foo(): void

返回

void

定义在

src/cheerio.spec.ts:13


myPlugin()

myPlugin(...args): object

参数

• ...args: unknown[]

返回

object

args

args: unknown[]

context

context: Cheerio<T>

定义在

src/cheerio.spec.ts:9


toArray()

toArray<T>(this): T[]

将 jQuery 集合中包含的所有 DOM 元素检索为数组。

类型参数

T

参数

this: Cheerio<T>

返回

T[]

包含的项目。

示例

$('li').toArray();
//=> [ {...}, {...}, {...} ]

定义在

src/api/traversing.ts:1027

遍历

add()

add<S, T>(this, other, context?): Cheerio<S | T>

将元素添加到匹配元素集中。

类型参数

S extends AnyNode

T extends AnyNode

参数

this: Cheerio<T>

other: string | S | Cheerio<S> | S[]

要添加的元素。

context?: string | Cheerio<S>

可选的新选择上下文。

返回

Cheerio<S | T>

组合集。

示例

$('.apple').add('.orange').length;
//=> 2

参见

https://api.jqueryjs.cn/add/

定义在

src/api/traversing.ts:1138


addBack()

addBack<T>(this, selector?): Cheerio<AnyNode>

将堆栈中的前一组元素添加到当前集合,可以选择通过选择器进行筛选。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

selector?: string

要添加的元素的选择器。

返回

Cheerio<AnyNode>

组合集。

示例

$('li').eq(0).addBack('.orange').length;
//=> 2

参见

https://api.jqueryjs.cn/addBack/

定义在

src/api/traversing.ts:1164


children()

children<T>(this, selector?): Cheerio<Element>

获取匹配元素集中每个元素的元素子元素。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

selector?: AcceptedFilters<Element>

如果指定,则筛选子元素。

返回

Cheerio<Element>

子元素。

示例

$('#fruits').children().length;
//=> 3

$('#fruits').children('.pear').text();
//=> Pear

参见

https://api.jqueryjs.cn/children/

定义在

src/api/traversing.ts:581


closest()

closest<T>(this, selector?): Cheerio<AnyNode>

对于集合中的每个元素,通过测试元素本身并在 DOM 树中向上遍历其祖先来获取第一个匹配选择器的元素。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

selector?: AcceptedFilters<Element>

要查找的元素的选择器。

返回

Cheerio<AnyNode>

最接近的节点。

示例

$('.orange').closest();
//=> []

$('.orange').closest('.apple');
// => []

$('.orange').closest('li');
//=> [<li class="orange">Orange</li>]

$('.orange').closest('#fruits');
//=> [<ul id="fruits"> ... </ul>]

参见

https://api.jqueryjs.cn/closest/

定义在

src/api/traversing.ts:341


contents()

contents<T>(this): Cheerio<AnyNode>

获取匹配元素集中每个元素的子元素,包括文本和注释节点。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

返回

Cheerio<AnyNode>

子元素。

示例

$('#fruits').contents().length;
//=> 3

参见

https://api.jqueryjs.cn/contents/

定义在

src/api/traversing.ts:604


each()

each<T>(this, fn): Cheerio<T>

遍历 cheerio 对象,为每个匹配的元素执行一个函数。当回调被触发时,函数是在 DOM 元素的上下文中触发的,因此 this 指的是当前元素,它等同于函数参数 element。要提前退出 each 循环,请返回 false

类型参数

T

参数

this: Cheerio<T>

fn

要执行的函数。

返回

Cheerio<T>

实例本身,可用于链式操作。

示例

const fruits = [];

$('li').each(function (i, elem) {
fruits[i] = $(this).text();
});

fruits.join(', ');
//=> Apple, Orange, Pear

参见

https://api.jqueryjs.cn/each/

定义在

src/api/traversing.ts:640


end()

end<T>(this): Cheerio<AnyNode>

结束当前链中最新的过滤操作,并将匹配元素集返回到其之前状态。

类型参数

T

参数

this: Cheerio<T>

返回值

Cheerio<AnyNode>

匹配元素集的先前状态。

示例

$('li').eq(0).end().length;
//=> 3

参见

https://api.jqueryjs.cn/end/

定义在

src/api/traversing.ts:1118


eq()

eq<T>(this, i): Cheerio<T>

将匹配元素集缩减到指定索引处的元素。使用 .eq(-i) 从最后一个选定元素反向计数。

类型参数

T

参数

this: Cheerio<T>

i: number

要选择的元素的索引。

返回值

Cheerio<T>

i 个位置的元素。

示例

$('li').eq(0).text();
//=> Apple

$('li').eq(-1).text();
//=> Pear

参见

https://api.jqueryjs.cn/eq/

定义在

src/api/traversing.ts:966


filter()

filter(this, match)

filter<T, S>(this, match): Cheerio<S>

遍历一个 Cheerio 对象,将选择器元素集缩减到匹配选择器或通过函数测试的元素。

这是使用类型保护的定义;查看下面有关调用此方法的其他方法。该函数在所选元素的上下文中执行,因此 this 指的是当前元素。

类型参数

T

S

参数

this: Cheerio<T>

match

要查找的值,遵循上面的规则。

返回值

Cheerio<S>

过滤后的集合。

示例
$('li')
.filter(function (i, el) {
// this === el
return $(this).attr('class') === 'orange';
})
.attr('class'); //=> orange
参见

https://api.jqueryjs.cn/filter/

定义在

src/api/traversing.ts:735

filter(this, match)

filter<T, S>(this, match): Cheerio<S extends string ? Element : T>

遍历一个 Cheerio 对象,将选择器元素集缩减到匹配选择器或通过函数测试的元素。

  • 当指定 Cheerio 选择器时,仅返回包含在该选择器中的元素。
  • 当指定元素时,仅返回该元素(如果它包含在原始选择器中)。
  • 如果使用函数方法,则函数在所选元素的上下文中执行,因此 this 指的是当前元素。
类型参数

T

S

参数

this: Cheerio<T>

match: S

要查找的值,遵循上面的规则。请参阅 AcceptedFilters

返回值

Cheerio<S extends string ? Element : T>

过滤后的集合。

示例
$('li').filter('.orange').attr('class');
//=> orange
$('li')
.filter(function (i, el) {
// this === el
return $(this).attr('class') === 'orange';
})
.attr('class'); //=> orange
参见

https://api.jqueryjs.cn/filter/

定义在

src/api/traversing.ts:774


find()

find<T>(this, selectorOrHaystack?): Cheerio<Element>

获取当前匹配元素集中每个元素的后代,通过选择器、jQuery 对象或元素进行过滤。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

selectorOrHaystack?: string | Element | Cheerio<Element>

要查找的元素。

返回值

Cheerio<Element>

找到的元素。

示例

$('#fruits').find('li').length;
//=> 3
$('#fruits').find($('.apple')).length;
//=> 1

参见

https://api.jqueryjs.cn/find/

定义在

src/api/traversing.ts:47


first()

first<T>(this): Cheerio<T>

将选择 Cheerio 对象的第一个元素。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

返回值

Cheerio<T>

第一个元素。

示例

$('#fruits').children().first().text();
//=> Apple

参见

https://api.jqueryjs.cn/first/

定义在

src/api/traversing.ts:925


get()

get(this, i)

get<T>(this, i): T | undefined

检索 Cheerio 对象匹配的元素之一,位于第 i 个位置。

类型参数

T

参数

this: Cheerio<T>

i: number

要检索的元素。

返回值

T | undefined

i 个位置的元素。

示例
$('li').get(0).tagName;
//=> li
参见

https://api.jqueryjs.cn/get/

定义在

src/api/traversing.ts:992

get(this)

get<T>(this): T[]

检索 Cheerio 对象匹配的所有元素,作为数组。

类型参数

T

参数

this: Cheerio<T>

返回值

T[]

Cheerio 对象匹配的所有元素。

示例
$('li').get().length;
//=> 3
参见

https://api.jqueryjs.cn/get/

定义在

src/api/traversing.ts:1007


has()

has(this, selectorOrHaystack): Cheerio<AnyNode | Element>

将匹配元素集过滤为仅包含给定 DOM 元素作为后代或其后代与给定选择器匹配的元素。等同于 .filter(':has(selector)')

参数

this: Cheerio<AnyNode>

selectorOrHaystack: string | Element | Cheerio<Element>

要查找的元素。

返回

Cheerio<AnyNode | Element>

过滤后的集合。

示例

$('ul').has('.pear').attr('id');
//=> fruits
$('ul').has($('.pear')[0]).attr('id');
//=> fruits

参见

https://api.jqueryjs.cn/has/

定义于

src/api/traversing.ts:899


index()

index<T>(this, selectorOrNeedle?): number

在匹配的元素中搜索给定的元素。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

selectorOrNeedle?: string | AnyNode | Cheerio<AnyNode>

要查找的元素。

返回

number

元素的索引。

示例

$('.pear').index();
//=> 2 $('.orange').index('li');
//=> 1
$('.apple').index($('#fruit, li'));
//=> 1

参见

https://api.jqueryjs.cn/index/

定义于

src/api/traversing.ts:1049


is()

is<T>(this, selector?): boolean

检查当前元素列表,如果任何元素匹配选择器,则返回true。如果使用元素或 Cheerio 选择,则返回true如果任何元素匹配。如果使用谓词函数,则函数在所选元素的上下文中执行,因此this引用当前元素。

类型参数

T

参数

this: Cheerio<T>

selector?: AcceptedFilters<T>

选择器的选择。

返回

boolean

选择器是否与实例的元素匹配。

参见

https://api.jqueryjs.cn/is/

定义于

src/api/traversing.ts:810


last()

last<T>(this): Cheerio<T>

将选择 cheerio 对象的最后一个元素。

类型参数

T

参数

this: Cheerio<T>

返回

Cheerio<T>

最后一个元素。

示例

$('#fruits').children().last().text();
//=> Pear

参见

https://api.jqueryjs.cn/last/

定义于

src/api/traversing.ts:943


map()

map<T, M>(this, fn): Cheerio<M>

将当前匹配集中每个元素传递给函数,生成一个包含返回值的新 Cheerio 对象。该函数可以返回单个数据项或要插入到结果集中的数据项数组。如果返回数组,则数组内的元素将插入到集中。如果函数返回 null 或 undefined,则不会插入任何元素。

类型参数

T

M

参数

this: Cheerio<T>

fn

要执行的函数。

返回

Cheerio<M>

映射的元素,包装在 Cheerio 集合中。

示例

$('li')
.map(function (i, el) {
// this === el
return $(this).text();
})
.toArray()
.join(' ');
//=> "apple orange pear"

参见

https://api.jqueryjs.cn/map/

定义于

src/api/traversing.ts:676


next()

next<T>(this, selector?): Cheerio<Element>

获取每个选定元素的下一个兄弟节点,可以选择通过选择器过滤。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

selector?: AcceptedFilters<Element>

如果指定为兄弟节点过滤。

返回

Cheerio<Element>

下一个节点。

示例

$('.apple').next().hasClass('orange');
//=> true

参见

https://api.jqueryjs.cn/next/

定义于

src/api/traversing.ts:396


nextAll()

nextAll<T>(this, selector?): Cheerio<Element>

获取每个选定元素的所有后续兄弟节点,可以选择通过选择器过滤。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

selector?: AcceptedFilters<Element>

如果指定为兄弟节点过滤。

返回

Cheerio<Element>

下一个节点。

示例

$('.apple').nextAll();
//=> [<li class="orange">Orange</li>, <li class="pear">Pear</li>]
$('.apple').nextAll('.orange');
//=> [<li class="orange">Orange</li>]

参见

https://api.jqueryjs.cn/nextAll/

定义于

src/api/traversing.ts:419


nextUntil()

nextUntil<T>(this, selector?, filterSelector?): Cheerio<Element>

获取所有后续兄弟节点,直到但不包括与选择器匹配的元素,可以选择通过另一个选择器过滤。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

selector?: null | AcceptedFilters<Element>

要停止的元素的选择器。

filterSelector?: AcceptedFilters<Element>

如果指定为兄弟节点过滤。

返回

Cheerio<Element>

下一个节点。

示例

$('.apple').nextUntil('.pear');
//=> [<li class="orange">Orange</li>]

参见

https://api.jqueryjs.cn/nextUntil/

定义于

src/api/traversing.ts:448


not()

not<T>(this, match): Cheerio<T>

从匹配元素集中删除元素。给定一个代表一组 DOM 元素的 Cheerio 对象,.not()方法从匹配元素的子集中构建一个新的 Cheerio 对象。提供的选择器将针对每个元素进行测试;不匹配选择器的元素将包含在结果中。

.not()方法可以像.filter()一样接受函数作为参数。对于函数返回true的元素将从过滤后的集中排除;所有其他元素都包含在内。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

match: AcceptedFilters<T>

要查找的值,遵循上面的规则。

返回

Cheerio<T>

过滤后的集合。

示例

$('li').not('.apple').length;
//=> 2
$('li').not(function (i, el) {
// this === el
return $(this).attr('class') === 'orange';
}).length; //=> 2

参见

https://api.jqueryjs.cn/not/

定义于

src/api/traversing.ts:858


parent()

parent<T>(this, selector?): Cheerio<Element>

获取当前匹配元素集中每个元素的父元素,可以选择通过选择器过滤。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

selector?: AcceptedFilters<Element>

如果指定,则过滤父级。

返回值

Cheerio<Element>

父级。

示例

$('.pear').parent().attr('id');
//=> fruits

参见

https://api.jqueryjs.cn/parent/

定义于

src/api/traversing.ts:245


parents()

parents<T>(this, selector?): Cheerio<Element>

获取当前匹配元素集中每个元素的父级,并通过 selector 进行过滤。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

selector?: AcceptedFilters<Element>

如果指定,则过滤父级。

返回值

Cheerio<Element>

父级。

示例

$('.orange').parents().length;
//=> 2
$('.orange').parents('#fruits').length;
//=> 1

参见

https://api.jqueryjs.cn/parents/

定义于

src/api/traversing.ts:271


parentsUntil()

parentsUntil<T>(this, selector?, filterSelector?): Cheerio<Element>

获取当前匹配元素集中每个元素的祖先,直到但不包括与选择器、DOM 节点或 Cheerio 对象匹配的元素。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

selector?: null | AcceptedFilters<Element>

要停止的元素的选择器。

filterSelector?: AcceptedFilters<Element>

可选的父级过滤。

返回值

Cheerio<Element>

父级。

示例

$('.orange').parentsUntil('#food').length;
//=> 1

参见

https://api.jqueryjs.cn/parentsUntil/

定义于

src/api/traversing.ts:305


prev()

prev<T>(this, selector?): Cheerio<Element>

获取每个选定元素的先前同级元素,可选地通过选择器进行过滤。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

selector?: AcceptedFilters<Element>

如果指定为兄弟节点过滤。

返回值

Cheerio<Element>

先前节点。

示例

$('.orange').prev().hasClass('apple');
//=> true

参见

https://api.jqueryjs.cn/prev/

定义于

src/api/traversing.ts:473


prevAll()

prevAll<T>(this, selector?): Cheerio<Element>

获取每个选定元素的所有先前同级元素,可选地通过选择器进行过滤。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

selector?: AcceptedFilters<Element>

如果指定为兄弟节点过滤。

返回值

Cheerio<Element>

先前节点。

示例

$('.pear').prevAll();
//=> [<li class="orange">Orange</li>, <li class="apple">Apple</li>]

$('.pear').prevAll('.orange');
//=> [<li class="orange">Orange</li>]

参见

https://api.jqueryjs.cn/prevAll/

定义于

src/api/traversing.ts:497


prevUntil()

prevUntil<T>(this, selector?, filterSelector?): Cheerio<Element>

获取所有先前同级元素,直到但不包括与选择器匹配的元素,可选地通过另一个选择器进行过滤。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

selector?: null | AcceptedFilters<Element>

要停止的元素的选择器。

filterSelector?: AcceptedFilters<Element>

如果指定为兄弟节点过滤。

返回值

Cheerio<Element>

先前节点。

示例

$('.pear').prevUntil('.apple');
//=> [<li class="orange">Orange</li>]

参见

https://api.jqueryjs.cn/prevUntil/

定义于

src/api/traversing.ts:526


siblings()

siblings<T>(this, selector?): Cheerio<Element>

获取匹配元素集中每个元素的同级元素(不包括元素本身),可选地通过选择器进行过滤。

类型参数

T extends AnyNode

参数

this: Cheerio<T>

selector?: AcceptedFilters<Element>

如果指定为兄弟节点过滤。

返回值

Cheerio<Element>

同级元素。

示例

$('.pear').siblings().length;
//=> 2

$('.pear').siblings('.orange').length;
//=> 1

参见

https://api.jqueryjs.cn/siblings/

定义于

src/api/traversing.ts:554


slice()

slice<T>(this, start?, end?): Cheerio<T>

获取匹配指定范围(从 0 开始的索引)的元素。

类型参数

T

参数

this: Cheerio<T>

start?: number

元素开始被选定的位置。如果为负数,则表示相对于集末尾的偏移量。

end?: number

元素停止被选定的位置。如果为负数,则表示相对于集末尾的偏移量。如果省略,则范围一直持续到集的末尾。

返回值

Cheerio<T>

匹配指定范围的元素。

示例

$('li').slice(1).eq(0).text();
//=> 'Orange'

$('li').slice(1, 2).length;
//=> 1

参见

https://api.jqueryjs.cn/slice/

定义于

src/api/traversing.ts:1095