Home  >  Article  >  Web Front-end  >  Two different ways to get parent elements and delete child elements using js and jquery_javascript skills

Two different ways to get parent elements and delete child elements using js and jquery_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:04:381238browse

var obj=document.getElementById("id"); What you get is the dom object. Use the js method when operating on the object

var obj=$("#id"); What you get is a jquery object. Use the jquery method when operating on this object

1. Traverse the objects obtained above

(1).js method for(vat i=0;j

(2).jquery method $(“#id”).each(function(){ $(this) to get the corresponding element});

2. Get the parent element of the element that meets the condition

(1).js method: var o=obj[i].parentNode

(2).jquery method: var o=$(this).parent()

2. After getting the parent element, you can delete the child elements of the parent element

(1).js method: o.removeChild(obj[i]);

(2).jquery method: o.empty()

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn