老生常谈jquery id选择器和class选择器的区别
投稿:jingxian
下面小编就为大家带来一篇老生常谈jquery id选择器和class选择器的区别。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
title Title /title
link href="style.css" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css"/
script type="text/javascript" src="jquery-2.1.4.js" /script
script type="text/javascript" src="dams.js"
/script
/head
body
div hello /div
div world /div
/body
/html
title Title /title
link href="style.css" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css"/
script type="text/javascript" src="jquery-2.1.4.js" /script
script type="text/javascript" src="dams.js"
/script
/head
body
div id="box" hello /div
div id="box" world /div
/body
/html
即: id是唯一的,即使有多个id相同的元素,jquery选择器也只能获取其中一个 。所以:想在jquery中对id设置动作, id在页面中只允许出现一次。
对于CSS样式来说,可以选取页面中所有id=box的DOM对象:
#box {
color: red;
jQuery选择器的写法和CSS选择器十分类似,但是功能却不同:
CSS找到元素后添加的是单一样式,而jquery添加的是动作行为。
以上这篇老生常谈jquery id选择器和class选择器的区别就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持凡科。