在 jquery 中它非常简单
例如
$("br").parent().contents().each(function() {
但是对于 nokogiri,xpath,
效果不太好
var = doc.xpath('//br/following-sibling::text()|//br/preceding-sibling::text()').map do |fruit| fruit.to_s.strip end
require 'rubygems'
require 'nokogiri'
doc = Nokogiri::HTML(DATA.read)
fruits = doc.xpath('//br/../text()').map { |text| text.content.strip }
p fruits
__END__
<html>
<body>
<div>
apple<br>
banana<br>
cherry<br>
orange<br>
</div>
</body>
我对 nokogiri 不熟悉,但是您是否试图找到包含 a 的任何元素的所有子元素<br/>
?如果是这样,请尝试:
//*[br]/node()
在任何情况下,使用 text() 只会匹配文本节点,而不是任何兄弟元素,这可能是也可能不是你想要的。如果您实际上只想要文本节点,那么
//*[br]/text()
应该做的伎俩。