【发布时间】:2017-05-07 09:57:22
【问题描述】:
我只是想在这个 HTML sn-p 的头部添加一个“虚拟”div,我已经尝试了 100 种方法,但没有任何效果。
这是head 拉下来时的样子:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Horraa</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
</head>
我只想放入这个虚拟 div:
<div> id='todd' class='{{customer.name}} {{shop.domain}}'</div>
所以最终的结果是这样的
<head>
<div> id='todd' class='{{customer.name}} {{shop.domain}}'</div>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Horraa</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
</head>
这是我的诺克切里
page = Nokogiri::HTML(todd)
head = page.css('head')
头现在是Nokogiri::XML::NodeSet
这行不通
div = "<div> id='todd' class='{{customer.name}} {{shop.domain}}'</div>"
head.push(div)
或者这个
div = "<div> id='todd' class='{{customer.name}} {{shop.domain}}'</div>"
update = Nokogiri::XML::Node.new('div', todd)
update['class'] = '{{customer.name}} {{shop.domain}}'
head.add_previous_sibling(update)
head << update
或者这个
head.add_next_sibling "<div> id='todd' class='{{customer.name}} {{shop.domain}}'</div>"
我已经尝试了其他 10 个,但是这太长了.. 我在哪里误入歧途?
【问题讨论】:
-
如果您在
div中的标记是有效的,它会更好地工作吗?你不能使用"<div>...",它必须是"<div..."。你提前终止了标签,所以 Nokogiri 不会接受它。