将 <a> 元素放在 document.write 中

put <a> element in document.write

本文关键字:document write 元素      更新时间:2023-09-26

我正在尝试做

document.write("<a href="http://www.google.com">Visit Google</a>");

但什么也没出现。我可以在 document.write 中不使用吗?其他 HTML 元素工作正常,例如

和 ,但这个元素什么都没有?有没有更好的链接方法?我也试过

[a link](http://www.example.com/) 

但仍然没有。

不要使用 document.write() ,请改为创建一个 anchor 元素。

var a = document.createElement('a');
a.href = 'http://www.google.com'
var text = document.createTextNode('Visit Google');
a.appendChild(text);
document.body.appendChild(a);

里面有双引号,请替换它们: document.write("<a href='http://www.google.com'>Visit Google</a>");

document.write('<a href="http://www.google.com">Visit Google</a>');

或像下面的帖子一样转义它们:

document.write("<a href='"http://www.google.com'">Visit Google</a>");