1. 程式人生 > >The differents of querySelector and getElmentbyId.

The differents of querySelector and getElmentbyId.

querySelector是返回指定元素節點中,子元素匹配的第一個,如果沒有匹配,則返回null。而querySelectorAll是在全文件中查詢所有匹配的項。querySelector是按照CSS來寫的,它獲取的都是靜態的元素,並且它的傳入值首位不能是數字。 獲取元素的方法有三種,分別是getElementById, getElementByClassName,getElementByTagName。它們分別返回DOM物件中對應ID,ClassName,TagName的元素。這三種方法都是動態的獲取元素。