Conversion between js node object and jQuery node object

1.js object is converted to jQuery object

When we first learned js, we knew that if we wanted to operate a certain node, we had to document.getElementById('oEle'); we all know that the id is unique in the html page. When there are multiple elements with the same id, the first is selected by default. One element. However, when the element does not have an id attribute, such as: document.getElementsByTagName('div'), a collection is selected, even if there is only one div on the page, it is a collection, and we cannot do a series of operations such as adding events for the collection. .

The conversion of js objects into jQuery objects is: $(jsObj)

<p id="oEle"></p>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript">
    let oEle =  document.getElementById('oEle');
    let oEle2 =  document.getElementsByTagName('p');
    console.log(oEle)//<p id=​"oEle">​
	console.log(oEle2)//HTMLCollection [p#oEle, oEle: p#oEle]
    console.log(oEle === oEle2) //false
    console.log(oEle === oEle2[0]) //true
    console.log(oEle instanceof jQuery);//false
	console.log($(oEle) instanceof jQuery);//true
</script>

 2. Convert jQuery object to JS object

<p id="oEle"></p>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript">
    let jqP = $("p");
    console.log(jqP instanceof jQuery)
    console.log(jqP[0] instanceof jQuery)//false
    console.log(jqP.get(0) instanceof jQuery)//false
    console.log(jqP.toArray()[0] instanceof jQuery)//false
</script>

It is a brief knowledge point, and I hope you can point out the wrong place.

Guess you like

Origin blog.csdn.net/m0_43599959/article/details/112386917