3 回答

TA貢獻1829條經驗 獲得超13個贊
您可能想使用document.querySelector:
var?right?=?document.querySelector('[href="https://xyz"]');
或者如果您需要n-th
匹配,document.querySelectorAll:
var?right?=?document.querySelectorAll('[href="https://xyz"]')[n];

TA貢獻2036條經驗 獲得超8個贊
首先我們要了解什么是html id屬性。
定義和用法
id 屬性指定 HTML 元素的唯一 id(該值在 HTML 文檔中必須是唯一的)。
id 屬性最常用于指向樣式表中的樣式,并通過 JavaScript(通過 HTML DOM)來操作具有特定 id 的元素。如何達到你的目的:
const barElement = document.getElementById('bar');//Getting the element which id is bar.
console.log(barElement);
const fooElement = barElement.parentNode;//Getting bars parent.
console.log(fooElement);
<div id="foo">
? <a id="bar" href="#"></a>
</div>

TA貢獻1789條經驗 獲得超10個贊
那是因為您沒有為該標簽分配任何 ID。因此 document.getElementById('?https://xyz?') 不會給你任何東西,因為沒有帶有此 ID 的標簽。
您必須像這樣分配一個 ID:
<...id="ID_of_href"?href="https://xyz'...>
然后你可以通過以下方式獲取它:
document.getElementById('ID_of_href')
- 3 回答
- 0 關注
- 138 瀏覽
添加回答
舉報