我需要像这样检查一个元素是否是最后一个子元素
Determining if the element is the last child of its parent
但我在 node.js 上使用 cheerio 而不是 jquery(为了更轻量)
https://github.com/MatthewMueller/cheerio
它给了我错误:
TypeError: Object XXXXXXX has no method 'is'
有人可以确认吗?如果是这样,检查 Node 是否是最后一个子 Node 的优雅方法是什么?
谢谢。
最佳答案
这是一个添加 isLastSibling
的示例检查元素是否是最后一个兄弟元素的方法。
var cheerio = require('cheerio'),
$ = cheerio.load('<p><a>1</a><b>2</b><i>3</i></p>'),
$fn = Object.getPrototypeOf($());
$fn.isLastSibling = function() {
return this.parent().children().last()[0] === this[0];
};
console.log(
$('a').isLastSibling(),
$('b').isLastSibling(),
$('i').isLastSibling()
);
你应该得到的输出是false false true
因为<a>
和 <b>
元素不是最后的 sibling ,而是 <i>
元素是。
关于javascript - .is() 在 Cheerio 中不存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15128582/