2015-04-23 100 views
1

我有一個文件:移動節點作爲另一個節點的第一個孩子

<html> 
<head> 
    <style>some styles<style> 
</head> 
<body> 
    <h1>Header</h1> 
    <table>table content</table> 
    <div>some text</div> 
</body> 
</A> 

下面的代碼移到下面<style>標籤<div><body>

style = @doc.at_css "style" 
    body = @doc.at_css "body" 
    style.parent = body 

有沒有一種辦法移動<style>以上<h1>

回答

3

找到身體的第一個孩子,並將樣式標籤添加爲之前的兄弟姐妹給第一個孩子解決問題。

style = @doc.at_css "style" 
    body = @doc.at_css "body" 
    style.parent = body 

    first_child = body.first_element_child 
    first_child.add_previous_sibling(style) 
相關問題