The previous_node
method is used to find the previous sibling node for the given element. This method is provided by the Mojo::DOM
module, which is an HTML/XML DOM parser with CSS selectors.
Note: Theprevious
method returns the previous sibling element, while theprevious_node
returns the previous sibling node.
$dom->previous_node
This method returns the previous node for the given element as a Mojo::DOM
object.
A sample HTML code is given below:
<div>Inside div<p id="a">Inside paragraph </p>After paragraph<h1>Inside h1</h1><h2>Inside h2</h2></div>
If we find the previous sibling node for the h1
element using the previous_node
method, we get the following output:
After paragraph
Note: Usingprevious
here would have returned<p id="a">Inside paragraph </p>
.
use 5.010;use Mojo::DOM;# Parse the htmlmy $dom = Mojo::DOM->new('<div>Inside div <p id="a">Inside paragraph </p>After paragraph<h1>Inside h1</h1><h2>Inside h2</h2></div>');# Get previous sibling nodesay $dom->at('h1')->previous_node
Mojo::DOM
module.$dom
.previous_node
method to get the previous sibling node for the h1
element, and print the returned Mojo::DOM
object.