querySelector在搜索id「#b> a時返回null,但getElementById返回正確的元素。這是怎麼回事?quertySelector返回空值是什麼?
var x = document.querySelector('#b>a');
console.log(x);
var y = document.getElementById("b>a");
console.log(y);
Name <input type="text" id="b>a">
querySelector在搜索id「#b> a時返回null,但getElementById返回正確的元素。這是怎麼回事?quertySelector返回空值是什麼?
var x = document.querySelector('#b>a');
console.log(x);
var y = document.getElementById("b>a");
console.log(y);
Name <input type="text" id="b>a">
您需要轉義>
字符。請參見下面的代碼片段
var x = document.querySelector('#b\\>a');
console.log(x);
var y = document.getElementById("b>a");
console.log(y);
Name <input type="text" id="b>a">
通過選擇查詢將尋找類型的角色前,所以它會尋找的,而不是文字。
爲了得到這個工作,你需要使用逃生
var x = document.querySelector('#b\\>a');
選擇爲什麼你需要,因爲它是高度預計麻煩運行ID這樣的名字? – reporter