当前位置: 移动技术网 > IT编程>开发语言>JavaScript > jquery和javascript的区别(常用方法教程比较)

jquery和javascript的区别(常用方法教程比较)

2019年03月25日  | 移动技术网IT编程  | 我要评论
1、加载dom区别

javascript:
window.onload

function first(){
alert('first');
}
function second(){
alert('second');
}
window.onload = first;
window.onload = second;
//只会执行第二个window.onload;不过可以通过以下方法来进行改进:
window.onload = function(){
first();
second();
}

jquery:
$(document).ready()

$(document).ready(){
function first(){
alert('first');
}
function second(){
alert('second');
}
$(document).ready(function(){
first();
}
$(document).ready(function(){
second();
}
//两条均会执行
}

2、获取id

javascript
document.getelementbyid('idname')

jquery:
$('#idname')

3、获取class

javascript:
javascript没有默认的获取class的方法

jquery:
$('.classname')

4、获取tagname

javascript:
document.getelementsbytagname('tagname')

jquery:
$('tagname')

5、创建对象并加入文档中

javascript: 
var para = document.createelement('p');
//创建一个p元素
document.body.appendelement(para);
//将p元素追加为body的lastchild子节点,如果想将新创建的p元素插入到已存在的某个元素之前,可以使用insertbefore()方法

jquery:
jquery提供了4种将新元素插入到已有元素(内部)之前或者之后的方法:append()、appendto()、prepend()、prependto()。
格式:$( html );
eg,html代码:
<p>world!</p>
$('p').append('<b>hello!</b>');
//输出:<p>world!<b>hello!</b></p>
$('<b>hello!</b>').appendto('p'); //输出:同上
$('p').prepend('<b>hello!</b>');
//输出:<p><b>hello!</b>world! </p>
$('<b>hello!</b>').prependto('p');
//输出:同上

6、插入新元素

javascript:
insertbefore() 语法格式:
parentelement.insertbefore(newelement,targetelement)
eg, 将一个img元素插入一个段落之前。

html代码:
<img src="image.jpg" id="imgs" />
<p>这是一段文字</p>

javascript代码:
var imgs = document.getelementbyid('imgs');
var para = document.getelementsbytag('p');
para.parenetnode.insertbefore(imgs,para);

jquery:
jquery提供了4种将新元素插入到已有元素(外部)之前或者之后的方法:after()、insertafter()、before()、insertbefore()。
格式:$( html );
eg,html代码:
<p>world!</p>

jquery代码
$('p').after('<b>hello!</b>');
//输出:<p>world! </p><b>hello!</b>
$('<b>hello!</b>'). insertafter ('p');
//输出:同上
$('p').before('<b>hello!</b>');
//输出:<b>hello!</b><p>world! </p>
$('<b>hello!</b>').insertbefore('p');
//输出:同上

7、复制节点

javascript:
reference = node.clonenode(deep)
这个方法只有一个布尔型的参数,它的可取值只能是true或者false。该参数决定是否把被复制节点的子节点也一同复制到新建节点里去。

jquery:
clone() //复制节点后,被复制的新元素并不具有任何行为
clone(true) //复制节点内容及其绑定的事件
备注:该方法通常与appendto()、prependto()等方法结合使用。

8、删除节点

javascript:
reference = element.removechild(node)
removechild()方法将一个给定元素里删除一个子节点

jquery:
remove();
remove()方法作用就是从dom中删除所有匹配的元素,remove()方法还可以与其他的过滤选择器结合使用,非常方便。
eg,将ul li下的title不是"hello"的li移除:
$('ul li').remove(li[title!='hello']);
empty();
empty()方法作用是清空节点。

9、包裹节点

javascript:
javascript暂无

jquery:
wrap() //将匹配元素用其他元素的结构化标记单独包裹起来
wrapall() //将所有匹配的元素用一个元素包裹起来
wrapinner() //将匹配元素的子内容用其他结构化的标记包裹起来

10、属性操作:设置属性节点、查找属性节点

javascript:
document.getelementsbytagname('tagname')

jquery:
jquery中设置和查找属性节点都是:attr() 。
$('p').attr('title'); //获取p元素的title属性;
$('p').attr('title','my title'); //设置p元素的title属性
$('p').attr('title':'my title','class':'myclass'); //当需要添加多个属性时,可以用"名:值"对的形式,中间用逗号隔开。

11、替换节点

javascript:
reference = element.replacechild(newchild,oldchild)
该方法是将一个给定父元素里的一个子节点替换为另外一个节点。

jquery:
replacewith()、replaceall()
eg:
<p>hello</p>
想替换为:
<h2>hi</h2>

jquery代码:
$('p') .replacewith('<h2>hi</h2>');
或者可以写成:
$('<h2>hi</h2>').replaceall('p');

12、css-dom操作

javascript:
格式:element.style.property
css-dom能够读取和设置style对象的属性,其不足之处是无法通过它来提取外部css设置的样式信息,而jquery的.css()方法是可以的。
注意点:css中的如"quot;font-size"这样有"-"的,要使用首字母小写的驼峰式表示,如fontsize。

jquery:
格式:$(selector).css()
css()方法获取元素的样式属性
此外,jquery还提供了height()和width()分别用来获取元素的高度和宽度(均不带单位),而css(height)、css(width)返回高宽,且带单位。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网