假设我有这个 HTML:
html = <div>Four score and seven years ago</div>
在“分数”一词之后插入(例如)锚标记的最佳方法是什么?注意:我想根据 DOM 操作(例如使用 Hpricot)而不是根据文本操作(例如,没有正则表达式)来执行此操作
假设我有这个 HTML:
html = <div>Four score and seven years ago</div>
在“分数”一词之后插入(例如)锚标记的最佳方法是什么?注意:我想根据 DOM 操作(例如使用 Hpricot)而不是根据文本操作(例如,没有正则表达式)来执行此操作
require 'rubygems'
require 'nokogiri'
doc = Nokogiri::XML(DATA)
text = doc.xpath('//text()').first
text.content =~ /^(.*score)(.*)$/
text.content = $1
node = Nokogiri::XML::Node.new('a',doc)
text.add_next_sibling node
node.add_next_sibling Nokogiri::XML::Text.new($2,doc)
puts doc.to_xml
__END__
<div>Four score and seven years ago</div>
我对红宝石不是很流利。但通常你应该有: Element div - 和 TextNode “四分七年前”
现在,如果你想插入一些东西,你将不得不: