gpt4 book ai didi

javascript - 动态创建链接 Javascript

转载 作者:可可西里 更新时间:2023-11-01 01:39:03 29 4
gpt4 key购买 nike

我试图将我的文本设置为一个链接,这样当我点击它时,它就会运行一个函数。现在我只是将它设置为 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

最佳答案

看这个例子:

http://jsfiddle.net/ajXEW/

我在代码中添加了一些注释来解释不同的步骤。

    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 = "#FF0000";
a = document.createElement('a');
a.href = 'google.com'; // Insted of calling setAttribute
a.innerHTML = "Link" // <a>INNER_TEXT</a>
leftDiv.appendChild(a); // Append the link to the div
document.body.appendChild(leftDiv); // And append the div to the document body

关于javascript - 动态创建链接 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9831074/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com