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")
操作 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 }
以上是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元素是否具有特定属性?在使用jQuery操作DOM元素时,经常会遇到需要判断元素是否具有某个特定属性的情况。这种情况下,我们可以借助jQuery提供的方法来轻松实现这一功能。下面将介绍两种常用的方法来判断一个jQuery元素是否具有特定属性,并附上具体的代码示例。方法一:使用attr()方法和typeof操作符//判断元素是否具有特定属

jQuery是一种流行的JavaScript库,被广泛用于处理网页中的DOM操作和事件处理。在jQuery中,eq()方法是用来选择指定索引位置的元素的方法,具体使用方法和应用场景如下。在jQuery中,eq()方法选择指定索引位置的元素。索引位置从0开始计数,即第一个元素的索引是0,第二个元素的索引是1,依此类推。eq()方法的语法如下:$("s

Oracle实例数量与数据库性能关系Oracle数据库是业界知名的关系型数据库管理系统之一,广泛应用于企业级的数据存储和管理中。在Oracle数据库中,实例是一个非常重要的概念。实例是指Oracle数据库在内存中的运行环境,每个实例都有独立的内存结构和后台进程,用于处理用户的请求和管理数据库的操作。实例数量对于Oracle数据库的性能和稳定性有着重要的影响。

jQuery是一个流行的JavaScript库,广泛用于网页开发中。在网页开发过程中,经常需要通过JavaScript动态地向表格中添加新行。本文将介绍如何使用jQuery为表格添加新行,并提供具体的代码示例。首先,我们需要在HTML页面中引入jQuery库。可以通过以下代码在标签中引入jQuery库:
