getElementById()和$("#id")的区别

1.getElementById()获取的是js对象,$("#id")获取到的是jq对象.

2.二者获取到的对象不同,所以只能操作使用其对应的js/jq对象的方法,不能使用jq对象去操作js的方法,也不能使用js对象去操作jq的方法.

3.两者相互转换

            * jq -- > js : jq对象[索引] 或者 jq对象.get(索引)
            * js -- > jq : $(js对象)


例:设置img标签的src属性

js:

<script type="text/javascript">
    //切换验证码
    function refreshCode(){
        //1.获取验证码图片对象
        var vcode = document.getElementById("vcode");

        //2.设置其src属性,加时间戳
        vcode.src = "${pageContext.request.contextPath}/checkCodeServlet?time="+new Date().getTime();
    }
</script>


jq:

<script>
        function refreshCode() {
           
$("#vcode").attr("src","${pageContext.request.contextPath}/checkCode?time="+new Date().getTime());
        }

注:如果使用jq对象是不能直接使用src方法操作img标签的

$("#vcode").src = "${pageContext.request.contextPath}/checkCodeServlet?time="+new Date().getTime();
上面这种写法是无法改变img的src属性值的.

     

猜你喜欢

转载自blog.csdn.net/weixin_41989775/article/details/81063161
今日推荐