-
-
Notifications
You must be signed in to change notification settings - Fork 905
From jQuery Manipulation
‹ Part of the “Nokogiri for jQuery Users” series
jQuery Manipulation methods (and their descriptions) along with Nokogiri equivalents.
Adds the specified class(es) to each of the set of matched elements.
Nokogiri (given name
= the class name):
node_set.add_class(name)
(See add_class.)
Or on just one node:
my_node['class'] ||= ""
my_node['class'] = my_node['class'] << " " << name
This should be less verbose, but more opaque — you are creating a NodeSet with just the one node, and then using NodeSet#add_class
:
Nokogiri::NodeSet.new(my_node.document, [my_node]).add_class(name)
Insert content, specified by the parameter, after each element in the set of matched elements.
In Nokogiri, it matters whether .after
is called on a Node
or NodeSet
. Calling .after
on a NodeSet
only inserts content after the last Node
in the NodeSet
(equivalent to my_node_set.last.after(content)
).
On a Node
, .after
works as you would expect.
On a NodeSet
, the jQuery equivalent would be:
my_node_set.each do |node|
node.after content
end
See also Node#add_next_sibling
.
Insert content, specified by the parameter, to the end of each element in the set of matched elements.
On one Node
:
my_node.add_child some_child # also returns the new child node
On a NodeSet
:
node_set.each do |node|
# create the new child perhaps using Nokogiri::XML::Builder
node.add_child new_child
end
Insert every element in the set of matched elements to the end of the target.
To document.
Get the value of an attribute for the first element in the set of matched elements.
Nokogiri: attr(key, value=nil, &blk)
Set the attribute key to
value
or the return value ofblk
(a block) on all Node objects in the NodeSet.
Also note that set
is an alias for attr
; and to remove an attribute, you can call remove_attr
.
Insert content, specified by the parameter, before each element in the set of matched elements.
Nokogiri .before
inserts the content only before the first Node
in a NodeSet
. See .after() on this page, and also Node#add_previous_sibling
.
Create a deep copy of the set of matched elements.
To document.
Get the value of a style property for the first element in the set of matched elements.
To document.
Remove the set of matched elements from the DOM.
Nokogiri NodeSet#remove
(an alias of NodeSet#unlink
).
Remove all child nodes of the set of matched elements from the DOM.
To document.
Determine whether any of the matched elements are assigned the given class.
To document.
Get the current computed height for the first element in the set of matched elements.
To document.
Get the HTML contents of the first element in the set of matched elements.
To document.
Get the current computed height for the first element in the set of matched elements, including padding but not border.
To document.
Get the current computed width for the first element in the set of matched elements, including padding but not border.
To document.
Insert every element in the set of matched elements after the target.
To document.
Insert every element in the set of matched elements before the target.
To document.
Get the current coordinates of the first element in the set of matched elements, relative to the document.
To document.
Get the current computed height for the first element in the set of matched elements, including padding, border, and optionally margin.
To document.
Get the current computed width for the first element in the set of matched elements, including padding and border.
To document.
Get the current coordinates of the first element in the set of matched elements, relative to the offset parent.
To document.
Insert content, specified by the parameter, to the beginning of each element in the set of matched elements.
On one Node
:
my_node.children.first.before content
# or perhaps:
my_node.children.first.add_previous_sibling node
On a NodeSet
:
node_set.each do |node|
node.children.first.before content
# Or perhaps
# create the new child perhaps using Nokogiri::XML::Builder
node.children.first.add_previous_sibling new_child
end
Insert every element in the set of matched elements to the beginning of the target.
To document.
Get the value of a property for the first element in the set of matched elements.
To document.
Remove the set of matched elements from the DOM.
Nokogiri NodeSet#remove
(an alias of NodeSet#unlink
).
Remove an attribute from each element in the set of matched elements.
To document.
Remove a single class, multiple classes, or all classes from each element in the set of matched elements.
To document.
Remove a property for the set of matched elements.
To document.
Replace each target element with the set of matched elements.
To document.
Replace each element in the set of matched elements with the provided new content.
To document.
Get the current horizontal position of the scroll bar for the first element in the set of matched elements.
To document.
Get the current vertical position of the scroll bar for the first element in the set of matched elements.
To document.
Get the combined text contents of each element in the set of matched elements, including their descendants.
To document.
Add or remove one or more classes from each element in the set of matched elements, depending on either the class's presence or the value of the switch argument.
To document.
Remove the parents of the set of matched elements from the DOM, leaving the matched elements in their place.
To document.
Get the current value of the first element in the set of matched elements.
To document.
Get the current computed width for the first element in the set of matched elements.
To document.
Wrap an HTML structure around each element in the set of matched elements.
To document.
Wrap an HTML structure around all elements in the set of matched elements.
To document.
Wrap an HTML structure around the content of each element in the set of matched elements.
To document.