【发布时间】:2012-09-13 13:29:05
【问题描述】:
我正在尝试访问 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 属性为何以及如何自动更改。
谁能解释一下这个问题?
【问题讨论】:
-
<a>元素在哪里?您正在使用/myservlet?myParam=foobar! 覆盖 HTML 内容 -
对不起,我写错了参数。我已经更正了有问题的更改。感谢您的评论。
-
console.log()输出什么?
标签: javascript html tags anchor