搜尋
首頁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 是透過<script></script>標籤插入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()  

  • #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,andleclassEsareSareSarereableFormultIllets.1)useIdIdSforuniqueElementsLikeAspeCificheader.2)useclassesforconsistentSistentSistentStyAcroSsmultipleLementslike.3)becautiouswithspecificitifieCificityAsiseSesses.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 ImproVereAdabilityActibility.2)and tagsallowsemlessallowseamelesseamlessallowseamelesseamlesseamelesseamemelessmultimedimeDiaiaembediiaembedplugins.3)。 3)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

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)