动态创建链接Javascript
Dynamically Create Link Javascript
我试图将文本设置为链接,这样当我点击它时,它就会运行一个函数。现在我只是把它设置在谷歌网站上,试图让文本显示为链接,但它似乎什么都没做。它只是静态文本。有什么建议吗?
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
试试这个:http://jsfiddle.net/HknMF/5/
var divColor = "red";
var fullName = "bob";
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');
a.appendChild(document.createTextNode(fullName + ' '));
leftDiv.appendChild(a); // Add name to left div
document.body.appendChild(leftDiv);
相关文章:
- JavaScript链接在点击时不起作用;函数在页面加载时工作
- 使用Java从javascript链接获取html更改
- 动态虚拟目录和外部 JavaScript 链接
- Web 地址虚拟目录会断开外部 JavaScript 链接,因此找不到文件
- Datatable Javascript链接在第2页不起作用
- 如何将href与javascript链接
- Javascript链接和变量替换
- Javascript链接使目标为空
- 外部JavaScript链接不起作用
- 鼠标光标改变Javascript链接的DIV
- Action使用Javascript链接并将值传递给控制器
- Javascript链接-试图设置元素值-奇怪的行为chrome&安卓
- 如何在没有onClick的情况下使用Javascript链接
- Javascript根据屏幕大小编写新的Javascript链接
- 更新用户's使用PHP或JavaScript链接
- JavaScript链接提取器
- javascript链接使页面在单击时向上
- 使用javascript链接样式
- 触摸设备的JavaScript链接预览
- Jquery javascript 链接含义