JS與 jQuery實例對比
本文主要和大家分享JS與 jQuery實例對比,主要以程式碼的形式和大家分享,希望能幫助大家。
DOM 屬性
// jQuery el.html() el.text() //取得所有匹配元素的内容 el.val() //获得匹配元素的当前值// JavaScript el.innerHTML() el.innerText() //老版本火狐不兼容 el.textContent() //老版本IE不兼容
DOM 查詢
1.获取页面所有p元素 // jQuery $("p") // JavaScript document.getElementsByTagName("p"); // IE4 + // Elements,表示此方法返回的是元素集合,有length属性,样式操作需配合索引 document.getElementsByTagName("p")[0].style.color = "blue";2.根据名称获取页面DOM // jQuery $("p[name = 'value']") // JavaScript document.getElementsByName("name")3.根据ID获取DOM节点 // jQuery $("#idName") // JavaScript document.getElementById("idName")4.根据类名获取DOM节点 // jQuery $(".className") // JavaScript document.getElementByClassName() //IE8 + document.querySelectorAll(".className") //返回元素集合 document.getElementsByClassName("className")5.根据选择器返回第一个匹配的节点(selectors CSS选择器,el DOM/元素对象) // jQuery $("selectors:first") // JavaScript, 非实时 document.querySelector("selectors") document.querySelectorAll("selectors")
操作Class
1.为DOM元素添加类 // jQuery $("selector").addClass("className"); // JavaScript el.classList.add("className");2.删除类 // jQuery $("selector").removeClass("className"); // JavaScript el.classList.remove("className");3.判断是否含有类,返回布尔值 true / false // jQuery $("selector").hasClass("classNames") // JavaScript el.classList.contains("classNames")
DOM 更改
1.尾部追加DOM元素(parent 父元素 ,child 子元素) // jQuery $("parent").append($("child")); // JavaScript var child = document.createElement("span"); //创建元素节点 var child = document.createTextNode("text"); //创建文本节点 child.appendChild(document.createTextNode("content")); //填充节点内容 parent.appendChild(child) //将node元素追加到尾部2.头部追加DOM元素 // jQuery $("parent").prepend($("child")); // JavaScript,剪切操作 appendChild() parent.insertBefore(a,b) //在 b 之前插入 a3.删除DOM元素 // jQuery $("child").remove(); // JavaScript child.remove() //彻底删除 el.removeChild(child); //删除child元素,返回被删元素,暂存对象3.替换DOM元素 // jQuery el.replaceWith("<b>Paragraph</b>") // JavaScript el.replaceChild(new,old)
新增樣式或屬性
1.添加CSS样式 // jQuery $("selector").css("color","#FFF"); //设置单个属性值 $("selector").css({"color":"#FFF","border":"1px",...}); //设置多个属性值 // JavaScript el.style.css="#FFF";2.获取CSS值 // jQuery $("selector").css("color"); //获取属性值 // JavaScript //只能获取内嵌CSS属性中的值(style=”…”),而无法获取外部引用CSS的属性 el.style.color; //返回RGB值3.添加属性 // jQuery $("selector").attr("id","main"); //设置单个属性值 $("selector").attr({"id":"main","name":"main",...}); //设置多个属性值 /* attr("attributeName",fn(v1,v2))的回调函数, v1为被选择元素的索引(index),v2为属性的旧值(oldvalue),返回属性新值 */ $("selector").attr("attributeName",function(index,oldvalue){ return }); //每次点击按钮,a元素的word自增1 //<a world = "1"></a> $("button").click(function(){ $("a").attr("world",function(index,v){return ~~v+1;}); }) // JavaScript(attributeName 属性名,attributeValue 属性值) el.setAttribute("attributeName","attributeValue");4.获取节点属性 // jQuery $("selector").attr("attributeName"); // JavaScript el.getAttribute("attributeName");5.移除节点属性 // jQuery $("selector").removeAttr("attributeName"); // JavaScript el.removeAttribute("attributename");
Event 事件
1.事件绑定(eventName 事件类型,fn(){} 事件处理函数) // jQuery $("selector").on("eventName", fn(){}); // JavaScript el.addEventListener("eventName", fn(){});2.解绑事件 // jQuery $("selector").on("eventName", fn(){}); // JavaScript el.removeEventListener("eventName", fn(){});
#顯示於隱藏
// jQuery $("selector").show(); //显示 $("selector").hide(); //隐藏// JavaScript el.style.display = ''; //显示 el.style.display = 'none'; //隐藏
頁面載入初始化
// jQuery $(function(){ //方式一 }) $(document).ready(function(){ //方式二 }) $().ready(function(){ //$() 函数 默认为 $(document) //方式二 }) (function($){ //方式三 - 闭包 })(jQuery)// JavaScript window.onload = function(){ //code }
#DOM 屬性
// jQuery el.html() el.text() //取得所有匹配元素的内容 el.val() //获得匹配元素的当前值// JavaScript el.innerHTML() el.innerText() //老版本火狐不兼容 el.textContent() //老版本IE不兼容
#################################################### 從####DOM 查詢######
1.获取页面所有p元素 // jQuery $("p") // JavaScript document.getElementsByTagName("p"); // IE4 + // Elements,表示此方法返回的是元素集合,有length属性,样式操作需配合索引 document.getElementsByTagName("p")[0].style.color = "blue";2.根据名称获取页面DOM // jQuery $("p[name = 'value']") // JavaScript document.getElementsByName("name")3.根据ID获取DOM节点 // jQuery $("#idName") // JavaScript document.getElementById("idName")4.根据类名获取DOM节点 // jQuery $(".className") // JavaScript document.getElementByClassName() //IE8 + document.querySelectorAll(".className") //返回元素集合 document.getElementsByClassName("className")5.根据选择器返回第一个匹配的节点(selectors CSS选择器,el DOM/元素对象) // jQuery $("selectors:first") // JavaScript, 非实时 document.querySelector("selectors") document.querySelectorAll("selectors")
1.为DOM元素添加类 // jQuery $("selector").addClass("className"); // JavaScript el.classList.add("className");2.删除类 // jQuery $("selector").removeClass("className"); // JavaScript el.classList.remove("className");3.判断是否含有类,返回布尔值 true / false // jQuery $("selector").hasClass("classNames") // JavaScript el.classList.contains("classNames")
1.尾部追加DOM元素(parent 父元素 ,child 子元素) // jQuery $("parent").append($("child")); // JavaScript var child = document.createElement("span"); //创建元素节点 var child = document.createTextNode("text"); //创建文本节点 child.appendChild(document.createTextNode("content")); //填充节点内容 parent.appendChild(child) //将node元素追加到尾部2.头部追加DOM元素 // jQuery $("parent").prepend($("child")); // JavaScript,剪切操作 appendChild() parent.insertBefore(a,b) //在 b 之前插入 a3.删除DOM元素 // jQuery $("child").remove(); // JavaScript child.remove() //彻底删除 el.removeChild(child); //删除child元素,返回被删元素,暂存对象3.替换DOM元素 // jQuery el.replaceWith("<b>Paragraph</b>") // JavaScript el.replaceChild(new,old)
1.添加CSS样式 // jQuery $("selector").css("color","#FFF"); //设置单个属性值 $("selector").css({"color":"#FFF","border":"1px",...}); //设置多个属性值 // JavaScript el.style.css="#FFF";2.获取CSS值 // jQuery $("selector").css("color"); //获取属性值 // JavaScript //只能获取内嵌CSS属性中的值(style=”…”),而无法获取外部引用CSS的属性 el.style.color; //返回RGB值3.添加属性 // jQuery $("selector").attr("id","main"); //设置单个属性值 $("selector").attr({"id":"main","name":"main",...}); //设置多个属性值 /* attr("attributeName",fn(v1,v2))的回调函数, v1为被选择元素的索引(index),v2为属性的旧值(oldvalue),返回属性新值 */ $("selector").attr("attributeName",function(index,oldvalue){ return }); //每次点击按钮,a元素的word自增1 //<a world = "1"></a> $("button").click(function(){ $("a").attr("world",function(index,v){return ~~v+1;}); }) // JavaScript(attributeName 属性名,attributeValue 属性值) el.setAttribute("attributeName","attributeValue");4.获取节点属性 // jQuery $("selector").attr("attributeName"); // JavaScript el.getAttribute("attributeName");5.移除节点属性 // jQuery $("selector").removeAttr("attributeName"); // JavaScript el.removeAttribute("attributename");
1.事件绑定(eventName 事件类型,fn(){} 事件处理函数) // jQuery $("selector").on("eventName", fn(){}); // JavaScript el.addEventListener("eventName", fn(){});2.解绑事件 // jQuery $("selector").on("eventName", fn(){}); // JavaScript el.removeEventListener("eventName", fn(){});
// jQuery $("selector").show(); //显示 $("selector").hide(); //隐藏// JavaScript el.style.display = ''; //显示 el.style.display = 'none'; //隐藏
// jQuery $(function(){ //方式一 }) $(document).ready(function(){ //方式二 }) $().ready(function(){ //$() 函数 默认为 $(document) //方式二 }) (function($){ //方式三 - 闭包 })(jQuery)// JavaScript window.onload = function(){ //code }
以上是JS與 jQuery實例對比的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

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

jQuery中如何使用PUT請求方式?在jQuery中,發送PUT請求的方法與發送其他類型的請求類似,但需要注意一些細節和參數設定。 PUT請求通常用於更新資源,例如更新資料庫中的資料或更新伺服器上的檔案。以下是在jQuery中使用PUT請求方式的具體程式碼範例。首先,確保引入了jQuery庫文件,然後可以透過以下方式發送PUT請求:$.ajax({u

jQuery如何移除元素的height屬性?在前端開發中,經常會遇到需要操作元素的高度屬性的需求。有時候,我們可能需要動態改變元素的高度,而有時候又需要移除元素的高度屬性。本文將介紹如何使用jQuery來移除元素的高度屬性,並提供具體的程式碼範例。在使用jQuery操作高度屬性之前,我們首先需要了解CSS中的height屬性。 height屬性用於設定元素的高度

標題:jQuery小技巧:快速修改頁面所有a標籤的文字在網頁開發中,我們經常需要對頁面中的元素進行修改和操作。使用jQuery時,有時候需要一次修改頁面中所有a標籤的文字內容,這樣可以節省時間和精力。以下將介紹如何使用jQuery快速修改頁面所有a標籤的文本,同時給出具體的程式碼範例。首先,我們需要引入jQuery庫文件,確保在頁面中引入了以下程式碼:<

標題:使用jQuery修改所有a標籤的文字內容jQuery是一款受歡迎的JavaScript庫,被廣泛用於處理DOM操作。在網頁開發中,經常會遇到需要修改頁面上連結標籤(a標籤)的文字內容的需求。本文將介紹如何使用jQuery來實現這個目標,並提供具體的程式碼範例。首先,我們需要在頁面中引入jQuery庫。在HTML檔案中加入以下程式碼:

jQuery是一種流行的JavaScript庫,被廣泛用於處理網頁中的DOM操作和事件處理。在jQuery中,eq()方法是用來選擇指定索引位置的元素的方法,具體使用方法和應用場景如下。在jQuery中,eq()方法選擇指定索引位置的元素。索引位置從0開始計數,即第一個元素的索引是0,第二個元素的索引是1,依此類推。 eq()方法的語法如下:$("s

如何判斷jQuery元素是否具有特定屬性?在使用jQuery操作DOM元素時,常會遇到需要判斷元素是否具有某個特定屬性的情況。在這種情況下,我們可以藉助jQuery提供的方法來輕鬆實現這項功能。以下將介紹兩種常用的方法來判斷一個jQuery元素是否具有特定屬性,並附上具體的程式碼範例。方法一:使用attr()方法和typeof運算子//判斷元素是否具有特定屬

Oracle實例數量與資料庫效能關係Oracle資料庫是業界知名的關係型資料庫管理系統之一,廣泛應用於企業級的資料儲存與管理。在Oracle資料庫中,實例是一個非常重要的概念。實例是指Oracle資料庫在記憶體中的運作環境,每個實例都有獨立的記憶體結構和後台進程,用於處理使用者的請求和管理資料庫的操作。實例數量對於Oracle資料庫的效能和穩定性有著重要的影響。

jQuery是一個受歡迎的JavaScript函式庫,廣泛用於網頁開發。在網頁開發過程中,經常需要透過JavaScript動態地在表格中新增一行。本文將介紹如何使用jQuery為表格新增一行,並提供具體的程式碼範例。首先,我們需要在HTML頁面中引入jQuery函式庫。可以透過以下程式碼在標籤中引入jQuery庫:
