【发布时间】:2023-04-06 18:34:01
【问题描述】:
我正在尝试将我的文本设置为链接,以便当我单击它时,它会运行一个函数。现在我只是将它设置为 google.com 以尝试让文本显示为链接,但它似乎根本没有做任何事情。这只是静态文本。有什么建议吗?
var leftDiv = document.createElement("div"); //Create left div
leftDiv.id = "left"; //Assign div id
leftDiv.setAttribute("style", "float:left; width:66.5%; line-height: 26px; text-align:left; font-size:12pt; padding-left:8px; height:26px;"); //Set div attributes
leftDiv.style.background = divColor;
a = document.createElement('a');
a.setAttribute('href', 'google.com');
user_name = a.appendChild(document.createTextNode(fullName + ' '));
leftDiv.appendChild(user_name); // Add name to left div
【问题讨论】:
-
我认为,指向另一个站点的链接必须使用完整的 URI/域名:
google.com需要为http://google.com,href才能链接到 Google。 -
它仍然显示为静态文本而不是链接。
-
您永远不会将链接插入到文档中,只会插入文本节点。
a.appendChild返回刚刚附加的节点。
标签: javascript href createelement