2017-05-04 1 views
0

h3 태그 뒤에 한 줄의 텍스트를 추가하려고합니다. 다른 태그 또는 문서 유형Nokogiri를 사용하여 div 다음에 텍스트를 추가하는 방법

<h3>hi</h3> 

:

내 시작 HTML이있다. 나는 나의 끝 HTML은 다음과 같이 할 :

page = Nokogiri::HTML::DocumentFragment.parse("my_page.html") 
title = page.at_css("h3") 
#tried this first 
title.children.after("\n{% include 'tester-credit-button' %}") 

#then this 
title << "\n{% include 'tester-credit-button' %}" 

#then this 
title.after("\n{% include 'tester-credit-button' %}") 

#then this 
text_node = Nokogiri::XML::Text.new("\n{% include 'tester-credit-button' %}" 
, page) 
title.add_child(text_node) 

답변

-2

당신은 너무 열심히 만들고있어 :

<h3>hi</h3> 
{% include 'tester-credit-button' %} 

나는 다음과 같은 노력

돌아 가기
require 'nokogiri' 
doc = Nokogiri::HTML::DocumentFragment.parse('<h3>hi</h3>') 
doc.at('h3').next = "\n{% include 'tester-credit-button' %}" 
puts doc.to_html 

# >> <h3>hi</h3> 
# >> {% include 'tester-credit-button' %} 

코드에. ..

page = Nokogiri::HTML::DocumentFragment.parse("my_page.html") 

입니다. 쉽게 사용하여 해당 테스트 할 수 있습니다 : 나는 HTML과 XML이 얼마나 공부하는 것이 좋습니다

page = Nokogiri::HTML::DocumentFragment.parse("<p><span>foo</span></p>") 
page.to_html # => "<p><span>foo</span></p>" 

:

page.to_html # => "my_page.html" 

the documentation, 그것은 말할 때 "tags는"당신이 어떤 HTML이나 XML에 전달하는 것을 의미합니다 구조화 된 문서의 노드, 가능한 유형 및 자녀 및 형제 자매의 개념을 이해하지 못했기 때문에 실험 결과가 구조화되었습니다. Nokogiri의 튜토리얼이 도움이 될 것입니다. 여기에서 태그를 검색하고 다양한 질문과 답변을 읽으십시오. Nokogiri는 강력하고 사용하기 쉽고 일단 당신이 벨트 아래서 그 개념을 얻으면.

+0

HTML, 노드 또는 노드 집합의 구조와 아무 관련이 없습니다. 그래도 제안 해 주셔서 감사합니다. – ToddT

관련 문제