に要素の値を変更することができません:ここでは、コードだ配列
HTML:
<div id="ID1">
<a href="www.google.com">click</a>
</div>
<a href="www.yahoo.com">click</a>
JS:
var element = document.querySelector("a[href^='www.google.com']");
console.log(element); // this returns the <a> object
element = element.parentNode;
console.log(element); // this returns the <div> object
は完璧に働きました。しかし、ここでの第二部です:
var elements = document.querySelectorAll("a[href^='www.google.com']");
console.log(elements[0]); //this returns <a> object
elements[0] = elements[0].parentNode;
console.log(elements[0]); //this returns <a> instead of <div>
だから、私はelements[0]
で値を変更することができませんでした。なぜこれが起こっているのですか? temp
のような変数を使用せずにどのように変更できますか?temp = elements[0]; temp = temp.parentNode; console.log(temp);
?
jqueryセレクタにclassとidを使用できませんか? – Gaetan
@Stagesそれでもうまくいくが、なぜこれがVanillaJSで失敗するのか不思議である。 –
@ gurvinder372いいえ、もう一度動作しません。コードの別の部分を別々に試してください。 –