Home  >  Article  >  Web Front-end  >  JQuery魔力之$("tagName")与selector_jquery

JQuery魔力之$("tagName")与selector_jquery

WBOY
WBOYOriginal
2016-05-16 17:55:361285browse
JQuery魔力(一)$("tagName")

DOM 中的 getElementsByTagName()方法在JQuery中的表现就是$("tagName")这么简单!

匿名函数来解决 window.onload 事件

对页面上的所有"div"标记下手,定义统一的外观

对"body"标记定义外观

动态添加一个"span"标记将其放置在"body"里

复制代码 代码如下:



JQuery 测试






第一个div


第二个div


第三个div




JQuery魔力(二) selector
tag标签(可以是:p、div、button …)标签本身具有ID、Class等属性
复制代码 代码如下:

$("tag") //取得页面中的某种标签的集合 同 document.getElementsByTagName("tag")
$("#id") //据id取得页面中的元素 同 document.getElementById("id")
$("tag#id") //据id取得标签为tag元素集合
$(".class") //据class取得元素集合 同 document.getElementsByClassName("class")
$("tag.class") //据class取得标签为tag的元素集合
$("tag1 tag2") //取得tag1内的tag2类型元素 同
$("tag1 > tag2")
$("tag1:has(tag2)") //取得包含tag2的tag1类型的元素集合

上面的selector充分体现了JQuery存在的价值,就是语法简洁且语义容易理解。
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn