如何使用Nokogiri为添加值?

时间:2021-06-07 20:29:23

I want to add content in a textarea while parsing HTML using Nokogiri:

我想在使用Nokogiri解析HTML时在textarea中添加内容:

<textarea placeholder="Describe" title="Describe" name="Describe" value=""></textarea>

Here is my code where I set the value for the textarea:

这是我的代码,我在其中设置textarea的值:

doc = Nokogiri::HTML( html_content )
textareas = doc.xpath("//textarea")

textareas.each do |r|
   r.set_attribute("value","Its my content")          
end

Here is the output:

这是输出:

<textarea placeholder="Describe" title="Describe" name="Describe" value="Its my content"></textarea>

I want output like this:

我想要像这样的输出:

<textarea placeholder="Describe" title="Describe" name="Describe" value=""> Its my content </textarea>

1 个解决方案

#1


3  

You can try:

你可以试试:

    doc = Nokogiri::HTML( html_content )
    textareas = doc.xpath("//textarea")

    textareas.each do |r|
       r.content = "Its my content"
    end

#1


3  

You can try:

你可以试试:

    doc = Nokogiri::HTML( html_content )
    textareas = doc.xpath("//textarea")

    textareas.each do |r|
       r.content = "Its my content"
    end