我想把文本设置为链接,这样当我点击它时,它会运行一个函数。现在我只是将它设置为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
google.com
需要变成http://google.com
,才能使href
链接到Google。 - David Thomasa.appendChild
返回刚刚添加的节点。 - Felix Kling