Methods to modify parent node attributes: 1. Use parent() to obtain the parent node object of the specified element, the syntax is "$(specified element).parent()"; 2. Use attr() to modify the parent node object The specified attribute, the syntax is "parent node object.attr({"attribute name":"value","attribute name":"value",...})".
The operating environment of this tutorial: windows7 system, jquery1.10.2 version, Dell G3 computer.
The jquery method of modifying the properties of the parent node
needs to be divided into two steps:
Get the specified The parent node object of the element
Modify the attributes of the selected element
The details will be introduced below.
1. Use the parent()
method to obtain the parent node object
parent() method returns the selected element 's direct parent element.
$(指定元素).parent(filter)
2. Use the attr() method to modify the specified attributes of the parent node object
attr() method can set the attributes of the selected element and value.
父节点对象.attr({"属性名":"属性值", "属性名":"属性值",...})
Example: Modify the id attribute of the parent node
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <style> .siblings,.siblings *{ display: block; border: 2px solid lightgrey; color: lightgrey; padding: 5px; margin: 15px; } #border{ border: 2px solid pink; } </style> <script src="js/jquery-1.10.2.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $("h2").css("border","2px solid red") $("button").click(function() { $("h2").parent().attr({"id":"border"}) }); }); </script> </head> <body> <div class="siblings">div (父) <p>p(兄弟元素)</p> <span>span(兄弟元素)</span> <h2>h2(本元素)</h2> <h3>h3(兄弟元素)</h3> <p>p(兄弟元素)</p> </div> <button>修改父节点的id属性</button> </body> </html>
[Recommended learning: jQuery video tutorial 、web front-end video】
The above is the detailed content of How to modify parent node attributes in jquery. For more information, please follow other related articles on the PHP Chinese website!