jquery 中的 $(“#”) 与 js中的document.getElementById(“”) 的区别
来源:互联网 发布:矩阵测光 编辑:程序博客网 时间:2024/06/06 08:39
jquery 中的 $(“#”) 与 js中的document.getElementById(“”) 的区别
以前没注意过,认为jquery 中的 $("#") 与 document.getElementById("") 是一回事,指的是同一个东西。
这次项目开发在使用验证码生成的时候,发现两者不同,使用时需要注意!通过以下测试:
js中代码
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();
}<img src="${pageContext.request.contextPath }/validCodeServlet" id="imageYZ" alt="换一张" onclick="reloadValidCode(this)"/>
看不清?<a href="#this" onclick="refresh();"> 换一张</a>
我很打算在refresh()函数中使用
$("#imageYz").src = "${pageContext.request.contextPath }/validCodeServlet?timed=" + new Date().getMilliseconds();
进行设置,但是发现不行;原来alert(o)的结果是:
进行设置,但是发现不行;原来alert(o)的结果是:
[object HTMLCanvasElement]
[objec Object]
从这里不难看出两者真的不一样;
从这里不难看出两者真的不一样;
再用firebug调试看一下,
$("#imageYZ")和document.getElementById("imageYZ")倒底是什么内容。调试结果如下:
$("#imageYZ") [img#imageYZ]
document.getElementById("imageYZ") img#imageYZ
想必,看到这里,不用我说,大家也会想到结果了。
实际上,$("#imageYZ")[0]等同于 document.getElementById("imageYZ")
$("#imageYZ")和document.getElementById("imageYZ")倒底是什么内容。调试结果如下:
$("#imageYZ") [img#imageYZ]
document.getElementById("imageYZ") img#imageYZ
想必,看到这里,不用我说,大家也会想到结果了。
实际上,$("#imageYZ")[0]等同于 document.getElementById("imageYZ")
0 0
- jquery 中的 $(“#”) 与 js中的document.getElementById(“”) 的区别
- jquery 中的 $(“#”) 与 js中的document.getElementById(“”) 的区别
- jquery 中的 $(“#”) 与 js中的document.getElementById(“”) 的区别
- jquery 中的 $(“#”) 与 js中的document.getElementById(“”) 的区别
- jquery 中的 $(“#”) 与 js中的document.getElementById(“”)细微区别
- jquery中的$("")与原生的document.getElementById("")的区别
- jquery中的 $(#id)与document.getElementById( id )的区别
- jquery 中的 $("#id") 与 document.getElementById("id") 的区别
- jquery中的$("#id")与document.getElementById("id")的区别
- jquery中的$("#id")与document.getElementById("id")的区别
- jquery中的 $(#id)与document.getElementById( id )的区别
- jquery中的$("#id")与document.getElementById("id")的区别
- jquery中的$("#id")与document.getElementById("id")的区别
- jquery中的 $(#id)与document.getElementById( id )的区别
- 【JavaScript】jQuery中的$("#id")与document.getElementById("id")的区别
- jquery 中的 $(“#”) 不等于 js中的document.getElementById(“”)
- jq中的$("#id")与js中的document.getElementById("id")的区别
- jquery中的$(this)和js的document.getElementById(this)的区别
- JSR 303 springmvc 数据校验
- Java反射机制实现与原理
- 微软面试100题-69
- UVA_11380_Down Went The Titanic
- 探究Lucene计算权重的过程
- jquery 中的 $(“#”) 与 js中的document.getElementById(“”) 的区别
- 数字在计算机中是如何存储的
- Product of Array except self | leetcode
- uCos-II内存管理
- AWS attach additional disk
- hdu_1532_Drainage Ditches
- Oracle 查询锁之间的依赖关系
- JS,Jquery获取各种屏幕的宽度和高度
- ubuntu 下配置Source Code Pro字体