3 回答

TA貢獻1827條經(jīng)驗 獲得超4個贊
我想了解jQuery對象和DOM元素之間的關(guān)系。
jQuery對象是一個類似數(shù)組的對象,包含DOM元素。jQuery對象可以包含多個DOM元素,具體取決于您使用的選擇器。
還有什么方法可以對jQuery對象和DOM元素進行操作?一個jQuery對象可以表示多個DOM元素嗎?
jQuery函數(shù)(完整列表在網(wǎng)站上)操作jQuery對象,而不是DOM元素。您可以使用以下方法訪問jQuery函數(shù)中的DOM元素.get()
或直接訪問所需索引中的元素:
$("selector")[0] // Accesses the first DOM element in this jQuery object
$("selector").get(0) // Equivalent to the code above
$("selector").get() // Retrieve a true array of DOM elements matched by this selector
換句話說,以下內(nèi)容會給您帶來相同的結(jié)果:
<div?id="foo"></div>alert($("#foo")[0]); alert($("#foo").get(0)); alert(document.getElementById("foo"));

TA貢獻1797條經(jīng)驗 獲得超6個贊
getElementById
var jQueryCollection = $("div"); //Contains all div elements in DOM
var normalCollection = document.getElementsByTagName("div");
div
get
var div1 = jQueryCollection.get(0); //Gets the first element in the collection

TA貢獻1824條經(jīng)驗 獲得超5個贊
大多數(shù)jQuery成員Functions
沒有返回值,而是返回當(dāng)前jQuery Object
或者另一個jQuery Object
.
所以,
console.log("(!!)?jquery?>>?"?+?$("#id")?)?;
會回來[object Object]
,即jQuery Object
維護集合,這是評估選擇器的結(jié)果。String
("#id"
)反對Document
,
同時,
console.log("(!!)?getElementById?>>?"?+?document.getElementById("id")?)?;
會回來[object HTMLDivElement]
(或事實上[object Object]
),因為/如果返回值是div
?Element
.
還有什么方法可以對jQuery對象和DOM元素進行操作?(1)一個jQuery對象可以表示多個DOM元素嗎?(2)
(1)有許多成員Function
在jQuery中,它與DOM有關(guān)。Object
最好的方法是在jQueryapi文檔中搜索相關(guān)的Function
一旦您有了特定的任務(wù)(例如選擇Node
s或操縱它們)。
jQuery文檔
(2)是的,一個單人jQuery Object
可以維護多個DOM的列表。Element
有多個Functions
(如jQuery.find
或jQuery.each
)建立在這種自動緩存行為的基礎(chǔ)上。
添加回答
舉報