最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Select sibling & parent with Cheerio - Stack Overflow

programmeradmin2浏览0评论

I've been unable to get access to really simple pieces of HTML using cheerio, I just finished opening up the object $(this) object and tracking down exactly what I need. Now an error occurs when there is no object in the stack, like a sibling or child. What's the correct way to select the previous sibling, get it's name, href & content, as well as the parent's css.

var code = $('code.lang-javascript, code.lang-js')
dataSet.blocks = []
code.map(function (i, elm) {
  var block = {}
  block.prevSiblingTag = $(this).parent().prev()[0].children[0].name
  block.prevSiblingHref = $(this).parent().prev()[0].children[0].attribs.href
  block.prevSiblingContent = $(this).parent().prev()[0].children[0].text()
  block.parentTag = $(this)[0].name
  block.parentClass = $(this)[0].attribs.class
  block.code = $(this).html()
  dataSet.blocks.push(block)
})

I was doing this and it was just a mess.

var $parent = $($(this).parent().html())
var $prevSibling = $($(this).parents().prev())
var block = {}
block.parentClass = $parent.attr('class')
block.prevSibling = $prevSibling.html()
block.code = $(this).html()

I've been unable to get access to really simple pieces of HTML using cheerio, I just finished opening up the object $(this) object and tracking down exactly what I need. Now an error occurs when there is no object in the stack, like a sibling or child. What's the correct way to select the previous sibling, get it's name, href & content, as well as the parent's css.

var code = $('code.lang-javascript, code.lang-js')
dataSet.blocks = []
code.map(function (i, elm) {
  var block = {}
  block.prevSiblingTag = $(this).parent().prev()[0].children[0].name
  block.prevSiblingHref = $(this).parent().prev()[0].children[0].attribs.href
  block.prevSiblingContent = $(this).parent().prev()[0].children[0].text()
  block.parentTag = $(this)[0].name
  block.parentClass = $(this)[0].attribs.class
  block.code = $(this).html()
  dataSet.blocks.push(block)
})

I was doing this and it was just a mess.

var $parent = $($(this).parent().html())
var $prevSibling = $($(this).parents().prev())
var block = {}
block.parentClass = $parent.attr('class')
block.prevSibling = $prevSibling.html()
block.code = $(this).html()
Share Improve this question asked Aug 17, 2015 at 6:17 ThomasReggiThomasReggi 59.7k97 gold badges259 silver badges459 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

The problem is if there is no children for the previous sibling then $(this).parent().prev()[0] will be null.

So a better to get the jQuery obeject reference to the target element then use it to get the attribute/property values like

var code = $('code.lang-javascript, code.lang-js')
dataSet.blocks = code.map(function (i, elm) {
    var block = {}, $this = $(this),
        target = $this.parent().prev().children().eq(0);
    block.prevSiblingTag = target.attr('name');
    block.prevSiblingHref = target.attr('href');
    block.prevSiblingContent = target.text()
    block.parentTag = this.name
    block.parentClass = $this.attr('class')
    block.code = $this.html()
    return block;
}).get();

Also since you are using .map(), it can return an array

发布评论

评论列表(0)

  1. 暂无评论