2017-02-11 37 views
0

如何使用jQuery的next()方法获取元素的同胞内容?我正在尝试将文字与Google搜索结果相关联。为此,我将链接存储在一个变量$('h3.r a')中。接下来我添加了一个点击处理程序,它会触发链接下抓取页面描述的代码。我期望$(this).next().html()$(this).html()相同的方式获得下一个兄弟姐妹的内容。不幸的是,我错了。这里是jQuery的,任何帮助真的看重jQuery获取使用next()的兄弟返回undefined

links.click(function() { 
    var nextElement = $(this).next() 
    console.log(nextElement.html()) //why doesn't this get contents? 

    var d = $(this).nextElementSibling 
    console.log(d) 
}) 

编辑:所以我检查控制台,当我登录$(this)

{ 
    "0":{ 
     "jQuery311027308429302958161":{ 
     "events":{ 
      "click":[ 
       { 
        "type":"click", 
        "origType":"click", 
        "data":null, 
        "guid":1, 
        "namespace":"" 
       } 
      ] 
     } 
     } 
    }, 
    "length":1 
} 

当我登录$(this).next()

{ 
    "length":0, 
    "prevObject":{ 
     "0":{ 
     "jQuery311027308429302958161":{ 
      "events":{ 
       "click":[ 
        { 
        "type":"click", 
        "origType":"click", 
        "data":null, 
        "guid":1, 
        "namespace":"" 
        } 
       ] 
      } 
     } 
     }, 
     "length":1 
    } 
} 
+1

'console.log''nextElement'并查看它的选择! – undefined

+1

'.nextElementSibling'不是一个jQuery方法。使用'this.nextSibling' – guest271314

回答

0

使用this.nextSibling选择下一个#text节点,.textContent选择节点的文本。