搜索
首页web前端前端问答javascript和jquery之间有什么区别

javascript和jquery之间有什么区别

Sep 07, 2021 pm 01:16 PM
javascriptjquery

区别:1、JavaScript是一种轻量级的编程语言,而JQuery是一个JavaScript函数库;2、js是利用DOM方法创建元素节点,jQuery使用$就可以直接创建DOM元素;3、操作元素节点的方法不同;4、操作属性节点的方法不同。

javascript和jquery之间有什么区别

本教程操作环境:windows7系统、javascript1.8.5&&jquery1.10.0版、Dell G3电脑。

一、本质上的区别

1.JavaScript 是通过3f1c4e4b6b16bbbd69b2ee476dc4f83a2cacc6d41bbb37262a98f745aa00fbf0标签插入到HTML页面,可由所有的现代浏览器执行的一种轻量级的编程语言。

2.JQuery是一个JavaScript函数库。或者说是JavaScript中最流行的一种框架。

使用JQuery首先要在 HTML 代码最前面加上对 jQuery 库的引用,比如:

<script src="js/jquery.min.js"></script>

库文件既可以放在本地,也可以直接使用知名公司的 CDN,好处是这些大公司的 CDN 比较流行,用户访问你网站之前很可能在访问别的网站时已经缓存在浏览器中了,所以能加快网站的打开速度。另外一个好处是显而易见的,节省了网站的流量带宽。例如:

<script src=></script>  //Google

或者:

<script src="http://code.jquery.com/jquery-1.6.min.js"></script>   //jQuery 官方 

二、语法上的差异

1.操作元素节点的方法不同

a.JavaScript使用

getElement系列、query系列

<body>
    <ul>
        <li id="first">哈哈</li>
        <li class="cls" name ="na">啦啦</li>
        <li class="cls">呵呵</li>
        <li name ="na">嘿嘿</li>
    </ul>
    <div id="div">
        <ul>
            <li class="cls">呵呵</li>
            <li>嘿嘿</li>
        </ul>
    </div>
</body>
<script>
  document.getElementById("first");        //是一个元素
  document.getElementsByClassName("cls");    //是一个数组,即使只有一个元素,使用时需要用[0]取到第一个再使用
  document.getElementsByName("na");       //是一个数组,即使只有一个元素,使用时需要用[0]取到第一个再使用  
  document.getElementsByTagName("li");     //是一个数组,即使只有一个元素,使用时需要用[0]取到第一个再使用  
  document.querySelector("#div");        //是一个元素   
  document.querySelectorAll("#div li");    //是一个数组,即使只有一个元素,使用时需要用[0]取到第一个再使用
</script

b.JQuery使用

大量的选择器同时使用$()包裹选择器

<body>
    <ul>
        <li id="first">哈哈</li>
        <li class="cls" name ="na">啦啦</li>
        <li class="cls">呵呵</li>
        <li name ="na">嘿嘿</li>
    </ul>
    <div id="div">
        <ul>
            <li class="cls">呵呵</li>
            <li>嘿嘿</li>
        </ul>
    </div>
</body>
<script src="http://code.jquery.com/jquery-1.6.min.js"></script>
<script>  //使用JQuery取到的是jquery对象都是一个数组,即使只有一个元素被选中,但是在使用时候不一定需要使用:eq(0)来拿到这一个在使用可以直接使用
    $("#first");            
    $(".cls");
    $("li type[name=&#39;na&#39;]");
    $("li");

    $("#div");
    $("#div li");
</script>

2.操作属性节点的方法不同

a.JavaScript使用 

getAttribute("属性名") 、 setAttribute("属性名","属性值")

<body>
    <ul>
        <li id=>哈哈</li>
    </ul>
</body>
<script>).getAttribute().setAttribute(,  document.getElementById("first").removeAttribute("name");
</script>

b.JQuery使用

.attr()传入一个参数获取,传入两个参数设置

.prop()

prop和attr一样都可以对文本的属性进行读取和设置;

两者的不同 在读取checked,disabled,等属性名=属性值的属性时

attr返回属性值或者undefined,当读取的checked属性时不会根据是否选中而改变

prop返回true和false 当读取的checked属性时会根据是否选中而改变

也就是说attr要取到的属性必须是在标签上写明的属性,否则不能取到

<body>
    <ul>
        <li id="first">哈哈</li>
    </ul>
</body><script src="js/jquery.js"></script>
<script>
  $("#first").attr("id");
  $("#first").attr("name","nafirst");  $("#first").removeAttr("name");
  $("#first").prop("id");   $("#first").prop("name","nafirst");   $("#first").removeProp("name");
</script>

3.操作文本节点的方法不同

a.JavaScript使用 

innerHTML:取到或设置一个节点的HTML代码,可以取到css,以文本的形式返回

innerText:取到或设置一个节点的HTML代码,不能取到css

value:取到input[type='text']输入的文本

<body>
    <ul>
        <li id="serven_times" ><span style="color: chartreuse">嘿嘿</span></li>
        <li id="eight_times" ><span style="color: chartreuse">嘿嘿</span> </li>
    </ul>
     姓名:<input type="text" id="input">
</body>
<script>
    document.getElementById("serven_times").innerHTML;
    document.getElementById("serven_times").innerHTML = "<span style=&#39;color: #ff3a29&#39;>呵呵</span>";
    document.getElementById("eight_times").innerText;
    document.getElementById("eight_times").innerText = "啦啦";
    document.getElementById("input").value;
</script>

b.JQuery使用

.html()取到或设置节点中的html代码
.text()取到或设置节点中的文本
.val()取到或设置input的value属性值

<body>
    <ul>
        <li id="serven_times" ><span style="color: chartreuse">嘿嘿</span></li>
        <li id="eight_times" ><span style="color: chartreuse">嘿嘿</span> </li>
    </ul>
     姓名:<input type="text" id="input">
</body>
<script src="/js/jquery.min.js"></script>
<script>
    $("#serven_times").html();
    $("#serven_times").html("<span style=&#39;color: #ff3a29&#39;>呵呵</span>");
    $("#eight_times").text();
    $("#eight_times").text("啦啦");
    $("#input").val();
    $("#input").val("哈哈");
</script>

4.操作css样式的方法不同

JavaScript:

* 1.使用setAttribute设置class和style
*   document.getElementById("first").setAttribute("style","color:red");
* 2.使用.className添加一个class选择器
*   document.getElementById("third").className = "san";
* 3.使用.style.样式直接修改单个样式。注意样式名必须使用驼峰命名法
*   document.getElementById("four_times").style.fontWeight = "900";
* 4.使用.style或.style.cssText添加一串行级样式:
*   document.getElementById("five_times").style = "color: blue;";//IE不兼容
*   document.getElementById("six_times").style.cssText = "color: yellow;font-size : 60px;";

JQuery:

$("#p2").css("color","yellow");

$("#p2").css({
    "color" : "white",
    "font-weight" : "bold",
    "font-size" : "50px",
});

5.操作层次节点

JavaScript:

*1.childNodes:获取当前节点的所有子节点(包括元素节点和文本节点)
*  children:获取当前节点的所有元素子节点(不包括文本节点)
*2.parentNode:获取当前节点的父节点
*3.firstChild:获取第一个元素节点,包括回车等文本节点
*  firstElementChild:获取第一个元素节点,不包括回车节点
*  lastChild、lastElementChild 同理
*4.previousSibling:获取当前元素的前一个兄弟节点
*  previousElementSibling::获取当前元素的前一个兄弟节点
*  nextSibling、nextElementSibling

JQuery:

1.提供了大量的选择器:

  • :first-child

  • :first-of-type1.9+

  • :last-child
  • :last-of-type1.9+

  • :nth-child

  • :nth-last-child()1.9+

  • :nth-last-of-type()1.9+

  • :nth-of-type()1.9+

  • :only-child

  • :only-of-type1.9+

2.除此之外也提供了对应的函数:

  • first()    

  • last()  

  • children()  

  • parents()  

  • parent()  

  • siblings() 

6.给一个节点绑定事件

JavaScript:

  使用了Dom0事件模型和Dom2事件模型,具体内容见我上一篇博客

JQuery:

  ①.事件绑定的快捷方式

<body>
    <button>按钮</button>
</body>
<script src="js/jquery-1.10.2.js"></script>
<script>
     $("button:eq(0)").click(function () {
        alert(123);
     });</script>

  ②:使用on进行事件绑定

<body>
    <button>按钮</button>
</body>
<script src="js/jquery-1.10.2.js"></script>
<script>
    //①使用on进行单事件的绑定
     $("button:eq(0)").on("click",function () {
        alert(456);
    });
     //②使用on同时给同一对象绑定多个事件
    $("button:eq(0)").on("click dblclick mouseover",function () {
        console.log(123);
    });
    //③使用on,给一个对象绑定多个事件
    $("button:eq(0)").on({
        "click":function () {
            console.log("click");
        },
        "mouseover":function () {
            console.log("mouseover");
        },
        "mouseover":function () {
            console.log("mouseover2");
        }
    });
    //④使用on给回调函数传参,要求是对象格式,传递的参数可以在e.data中取到;jquery中的e只能通过参数传进去,不能用window.event
    $("button:eq(0)").on("click",{"name":"zhangsan","age":15},function (e) {
        console.log(e);
        console.log(e.data);
        console.log(e.data.name);
        console.log(e.data.age);
        console.log(window.event);//js中的事件因子

    });
    
</script>

7.JQuery的文档就绪函数和window.onload的区别

*①.window.onload必须等待网页资源(包括图片等)全部加载完成后,才能执行;
*      而文档就绪函数只需要等到网页DOM结构加载完成后,即可执行
*②.window.onload在一个页面中,只能写一次,写多次会被最后一次覆盖
*      而文档就绪函数在一个页面中可以有N个

三、JavaScript对象和JQuery对象的方法不能混用。

1.JavaScript对象和JQuery对象

① 使用$("")取到的节点为JQuery对象,只能调用JQuery方法,不能调用JavaScript方法;
*      $("#p").click(function(){})√
*      $("#p").onclick = function(){}× 使用JQuery对象调用JavaScript方法
*
*      同理,使用、document.getElement系列函数取到的对象为JavaScript对象,也不能调用JQery函数

2.JavaScript对象和JQuery对象互转

*① JQuery --->  JavaScript :使用get(index)或者[index]选中的就是JavaScript对象
*  $("p").get(0).onclick = function(){}
*  $("p").[0].onclick = function(){}
* ② JavaScript ---> JQuery :使用$()包裹JavaScript对象        (我们发现JQuery不管获得几个对象都是一个数组,可以直接给整个数组都添加某一事件)
*  var p = document.getElementById("p");
*  $(p).click(function(){});

【推荐学习:javascript高级教程

以上是javascript和jquery之间有什么区别的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
掌握CSS选择器:高效样式的类别与ID掌握CSS选择器:高效样式的类别与IDMay 16, 2025 am 12:19 AM

使用类选择器和ID选择器取决于具体用例:1)类选择器适用于多元素、可重用样式,2)ID选择器适用于唯一元素、特定样式。类选择器更灵活,ID选择器处理速度更快但可能影响代码维护性。

HTML5规范:探索关键目标和动机HTML5规范:探索关键目标和动机May 16, 2025 am 12:19 AM

keykeygoalsandmotivationsbehindhtml5weretoenhancesemantstructure,Improvemultimediasupport,andensureBetterperformanceandCompatibalityAcroscaroscaroscaroscarossdecrossdecrossdecrossdecrossdecrossdecrossdecrossdevices,drivendybytheneedtoAddresshtml4'slimitationsand limitiTations and limittations andmeetmeetModerntructAndmmoderntructss.1)

CSS ID和类:简单指南CSS ID和类:简单指南May 16, 2025 am 12:18 AM

IDSareNiqueAndusedForsingLelement,andleclassEsareReusableFormultPirultElements.1)useIdIdSforuniqueElementsLikeAspeCificheader.2)useclassesforconsistentSistentSistentStyActStyAcroSsmultipleLementslike.3)becautiouswithspecificitificitieAsideCerrrase.4)

HTML5目标:了解规范的关键目标HTML5目标:了解规范的关键目标May 16, 2025 am 12:16 AM

html5aimstoenhancewebaccctible,互动性和效率。1)ITSupportsMultimediawithOutPlugins,Simplifyinginguserexperience.2)Semanticmarkmarksmarkupimprovissupimprovessupstructureandacccessessible.3)增强bacegencementingIncrassubility.4)

使用HTML5难以实现其目标吗?使用HTML5难以实现其目标吗?May 16, 2025 am 12:06 AM

html5isnotparticulllydifficulttousebutrequirequireSustingingItsFeatures.1)smanticelementslike like ,,,和iMproveructure,andimprovucture,可读性,seo和acctibility.2)多中性倍增量,且可读性

CSS:我可以在同一DOM中使用多个ID吗?CSS:我可以在同一DOM中使用多个ID吗?May 14, 2025 am 12:20 AM

No,youshouldn'tusemultipleIDsinthesameDOM.1)IDsmustbeuniqueperHTMLspecification,andusingduplicatescancauseinconsistentbrowserbehavior.2)Useclassesforstylingmultipleelements,attributeselectorsfortargetingbyattributes,anddescendantselectorsforstructure

HTML5的目的:创建一个更强大,更容易访问的网络HTML5的目的:创建一个更强大,更容易访问的网络May 14, 2025 am 12:18 AM

html5aimstoenhancewebcapabilities,Makeitmoredynamic,互动,可及可访问。1)ITSupportsMultimediaElementsLikeAnd,消除innewingtheneedtheneedtheneedforplugins.2)SemanticeLelelemeneLementelementsimproveaCceccessibility inmproveAccessibility andcoderabilitile andcoderability.3)emply.3)lighteppoperable popperappoperable -poseive weepivewebappll

HTML5的重要目标:增强网络开发和用户体验HTML5的重要目标:增强网络开发和用户体验May 14, 2025 am 12:18 AM

html5aimstoenhancewebdevelopmentanduserexperiencethroughsemantstructure,多媒体综合和performanceimprovements.1)SemanticeLementLike like,和ImproVereAdiability and ImproVereAdabilityAncccossibility.2)和TagsallowsemplowsemplowseamemelesseamlessallowsemlessemlessemelessmultimedimeDiaiiaemediaiaembedwitWithItWitTplulurugIns.3)

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

北端:融合系统,解释
1 个月前By尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆树的耳语 - 如何解锁抓钩
4 周前By尊渡假赌尊渡假赌尊渡假赌
<🎜>掩盖:探险33-如何获得完美的色度催化剂
2 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用