本文共 2844 字,大约阅读时间需要 9 分钟。
通过 document.getElementById()
、document.getElementsByName()
等方法获取的都是 DOM 对象或 DOM 对象集合。
document.getElementById("text11")
返回与 id 为 "text11" 的唯一 DOM 对象。document.getElementsByName("text11")[0]
返回 id 为 "text11" 的 DOM 对象(如果存在)。document.all.text11
可以通过 name 或 id 值获取 DOM 对象。document.all[10]
根据索引获取 DOM 对象。JQuery 包装集通过 $()
方法构造,返回的对象可以调用 JQuery 方法。
$("#text11")
。$(text11_dom)
。.length
,但某些属性方法需要通过索引访问,如 $("#text11")[0]
。JQuery 的核心方法包括:
jQuery(html[, ownerDocument])
:通过 HTML 字符串创建 DOM 元素。jQuery(elements)
:将 DOM 对象或 JQuery 包装集封装为 JQuery 包装集。jQuery(callback)
:$(document).ready()
的简写,用于 DOM 加载后执行回调。jQuery(selector[, context])
:根据 CSS 选择器查找 JQuery 包装集,context
可以为 DOM 对象或 JQuery 包装集。$("input")
。$("#text11")
。$(".text11")
。$("#text11,.text12")
。$("*")
。tr
标签中获取下方所有 id 为 "text11" 的元素:$("tr #text11")
。td
标签下的直接 input
子元素:$("td > input")
。$("#text11 + .button11")
。$("#text11 ~ .button11")
。input
元素:$("input:first")
。input
元素:$("input:last")
。input
元素:$("input:not(:checked)")
。input
元素:$("input:even")
。input
元素:$("input:odd")
。input
元素:$("input:eq(1)")
。input
元素:$("input:gt(0)")
。input
元素:$("input:lt(2)")
。<h>
标签:$(":header")
。$(":animated")
。<h1>
元素:$("h1:contains('你好世界!')")
。<td>
元素:$("td:empty")
。input
子元素的 <td>
元素:$("td:has(input)")
。<td>
元素:$("td:parent")
。input
元素:$("input:hidden")
。input
元素:$("input:visible")
。input
元素:$("input[id]")
。input
元素:$("input[name='text11']")
。input
元素:$("input[name!='text11']")
。input
元素:$("input[name^='text']")
。input
元素:$("input[name$='11']")
。input
元素:$("input[name*='ext']")
。input
元素:$("input[id][name*='ext']")
。$("input:nth-child(2)")
。$("input:first-child")
。$("input:last-child")
。$("input:only-child")
。input
元素:$(":input")
。$(":text")
。$(":password")
。$(":checkbox")
。$(":submit")
。$(":image")
。$(":reset")
。$(":button")
。$(":file")
。input
元素:$("input:enabled")
。input
元素:$("input:disabled")
。$("input:checked")
。$("option:selected")
。$('#td1,#td2,p').css('color','red');
$('tbody td[attr1!="a1"]')
。$('[attr1="a1"][attr2="a2"]')
。转载地址:http://qnjmz.baihongyu.com/