Dynamically Create Link Javascript

后端 未结 2 1843
悲&欢浪女
悲&欢浪女 2020-12-16 15:30

I\'m trying to set my text as a link so that when I click on it, it runs a function. Right now I just have it set to google.com to try to get the text to appear as a link, b

2条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-16 15:45

    Try this out: 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);
    

提交回复
热议问题