怎么放一组<p>在 <div> 里面 [英] How to put a group of <p> inside a <div>

查看:68
本文介绍了怎么放一组<p>在 <div> 里面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过使用以下 Ruby 代码和 Nokogiri:

需要'rubygems'需要'nokogiri'value = Nokogiri::HTML.parse(<<-HTML_END)"<身体><p id='1'>A</p><p id='2'>B</p><h1>Bla</h1><p id='3'>C</p><p id='4'>D</p><p id='5'>E</p></html>"HTML_END# selected-array 由应用程序给出.# 它由一个排序的数组组成,所有 id 为# <p>需要用<div>括起来的选择 = ["2","3","4"]first_p = selected.firstlast_p = selected.last## 我需要在此处插入什么 Ruby 代码才能获得# 生成的 HTML 如下所示?#

生成的 HTML 应如下所示(请注意插入的 <div id='XYZ'>):

<身体><p id='1'>A</p><div id='XYZ'><p id='2'>B</p><h1>Bla</h1><p id='3'>C</p><p id='4'>D</p>

<p id='5'>E</p></html>

解决方案

这是我在我的项目中实施的工作解决方案(Vlad@SO & Whitelist@irc#rubyonrails:感谢您的帮助和启发.):

需要'rubygems'需要'nokogiri'value = Nokogiri::HTML.parse(<<-HTML_END)"<身体><p id='1'>A</p><p id='2'>B</p><h1>Bla</h1><p id='3'>C</p><p id='4'>D</p><p id='5'>E</p></html>"HTML_END# selected-array 由应用程序给出.# 它由一个排序的数组组成,所有 id 为# <p>需要用<div>括起来的选择 = ["2","3","4"]# 我们想要一个元素,而不是节点集!# .first 返回 Nokogiri::XML::Element 而不是 Nokogiri::XML::nodesetfirst_p = value.css("p##{selected.first}").firstlast_p = value.css("p##{selected.last}").firstparent = value.css('body').first# 构建并设置新的 div_nodediv_node = Nokogiri::XML::Node.new('div', value)div_node['class'] = 'XYZ'# 在 first_p 之前添加 div_nodefirst_p.add_previous_sibling(div_node)selected_node = falseparent.children.each 做 |tag|# 如果是第一个_pselected_node = true 如果 selected.include?标签['id']# 如果介于 first_p 和 last_p 之间div_node.add_child(tag) 如果 selected_node# 如果是最后一个_pselected_node = false if selected.last == tag['id']结尾把 value.to_html

I'd like to figure out a way to get to the HTML result (mentioned further below) by using the following Ruby code and Nokogiri:

require 'rubygems'
require 'nokogiri'

value = Nokogiri::HTML.parse(<<-HTML_END)
  "<html>
    <body>
      <p id='1'>A</p>
      <p id='2'>B</p>
      <h1>Bla</h1>
      <p id='3'>C</p>
      <p id='4'>D</p>
      <p id='5'>E</p>
    </body>
  </html>"
HTML_END

# The selected-array is given by the application.
# It consists of a sorted array with all ids of 
# <p> that need to be enclosed by the <div>
selected = ["2","3","4"]
first_p = selected.first
last_p = selected.last

#
# WHAT RUBY CODE DO I NEED TO INSERT HERE TO GET
# THE RESULTING HTML AS SEEN BELOW?
#

The resulting HTML should look like this (please note the inserted <div id='XYZ'>):

<html>
  <body>
    <p id='1'>A</p>
    <div id='XYZ'>
      <p id='2'>B</p>
      <h1>Bla</h1>
      <p id='3'>C</p>
      <p id='4'>D</p>
    </div>
    <p id='5'>E</p>
  </body>
</html>

解决方案

This is the working solution I've implemented into my project (Vlad@SO & Whitelist@irc#rubyonrails: Thanks for your help and inspiration.):

require 'rubygems'
require 'nokogiri'

value = Nokogiri::HTML.parse(<<-HTML_END)
  "<html>
    <body>
      <p id='1'>A</p>
      <p id='2'>B</p>
      <h1>Bla</h1>
      <p id='3'>C</p>
      <p id='4'>D</p>
      <p id='5'>E</p>
    </body>
  </html>"
HTML_END

# The selected-array is given by the application.
# It consists of a sorted array with all ids of 
# <p> that need to be enclosed by the <div>
selected = ["2","3","4"]

# We want an elements, not nodesets!
# .first returns Nokogiri::XML::Element instead of Nokogiri::XML::nodeset
first_p = value.css("p##{selected.first}").first
last_p = value.css("p##{selected.last}").first
parent = value.css('body').first

# build and set new div_node
div_node = Nokogiri::XML::Node.new('div', value)
div_node['class'] = 'XYZ'

# add div_node before first_p
first_p.add_previous_sibling(div_node)

selected_node = false

parent.children.each do |tag|
  # if it's the first_p
  selected_node = true if selected.include? tag['id']
  # if it's anything between the first_p and the last_p
  div_node.add_child(tag) if selected_node
  # if it's the last_p
  selected_node = false if selected.last == tag['id']
end

puts value.to_html

这篇关于怎么放一组&amp;lt;p&gt;在 &amp;lt;div&gt; 里面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆