簡體   English   中英

錨元素的href屬性自動更改

[英]href attribute of anchor element changed automatically

我正在嘗試訪問HTML <a>元素的href屬性,但是不知何故該值會自動更改。

以下是我的代碼:

function getTDElement(anchorString)
{
  var td = document.createElement('td');
  // i think this is not a good way to add child to html element but 
  // i have to do it for some unavoidable reason
  td.innerHTML = anchorString;
  var anchor = td.firstChild;
  // following line prints url like
  // http://localhost/myservlet?myParam=foobar
  console.log(anchor.href);
  return td;
}

// im passing only /myservlet?myParam=foobar in following line
getTDElement("<a href=/myservlet?myParam=foobar>display</a>");

我無法理解元素的href屬性為何以及如何自動更改。

誰能說明這個問題?

鏈接元素上的href屬性是一個特殊屬性,而不是簡單的字符串。 可以將href值更改為它認為解析的絕對URL。 您可以使用getAttribute獲得不變的值。

console.log(anchor.getAttribute('href'));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM