[英]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.