The difference jquery $ ( "#") in the document.getElementById js ( "") of

Did not notice before, believe in jquery $ ( "#") and document.getElementById ( "") is one thing, referring to the same thing.

The project development in the use of code generation, both found different, you need to pay attention when using!

By the following tests:
JS code

function reloadValidCode(o) { 

alert (o);

    o.src = "${pageContext.request.contextPath }/validCodeServlet?timed=" + new Date().getMilliseconds(); 

}

 

function refresh() { 

alert($("#imageYZ"));

   document.getElementById("imageYZ").src = "${pageContext.request.contextPath }/validCodeServlet?timed=" + new Date().getMilliseconds(); 

}

jsp code

<img src="${pageContext.request.contextPath }/validCodeServlet" id="imageYZ" alt="换一张" onclick="reloadValidCode(this)"/>

 See?  <a href="#this" onclick="refresh();">  change a </a>


I'm going to use the refresh () function
. $ ( "# imageYz") the src =  "? $ {} pageContext.request.contextPath / validCodeServlet Timed ="  +  new new  a Date () the getMilliseconds ();. set, but not found; original results alert (o) is : 
[object HTMLCanvasElement]

The alert ($ ( "# imageYZ")) The result is:

[objec Object]
here it is easy to see two really different;
And then look at the firebug debugging,
$ ( "# imageYZ") and document.getElementById ( "imageYZ") what the hell is. Debugging results are as follows:

$ ( "# imageYZ") [img # imageYZ]
    
document.getElementById ( "imageYZ") img # imageYZ

presumably, see here, I do not say, we will think of the results.

Indeed, $ ( "# imageYZ") [0] is equivalent to document.getElementById ( "imageYZ")
 
 
 
You see here is jquery, $ (id) is obtained jquery object, it is an object one pair of packaging dom, html () method is a jquery object.
document.getElementById ( "errorPsd") is the dom object can be seen as javascript own object.

$ ( "# errorPsd") htm ();. is equivalent to document.getElementById ( "errorPsd") the innerHTML.

$ ( "# errorPsd") HTML ( "Change the span content");. is equivalent to document.getElementById ( " errorPsd ".) innerHTML =" change the span content ";
more content you can refer to the help documentation jquery

Guess you like

Origin www.cnblogs.com/gongxianjin/p/11246466.html