最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Creating anchor tags dynamically in HTML using JavaScript - Stack Overflow

programmeradmin6浏览0评论

I have a question about anchor tags and javascript.Converting a URL to an anchor tag

The text box accepts a url (eg. "www.youtube")

I made a Javascript function that adds "http://" to the link.

How do I make it so the convert button adds a link on the webpage that takes you to the website in another tab.

My Javascript code is as follows:

var webpage="";
var url="";
var message="";
var x= 0;
var page="";

function convert()
{       
    url=document.getElementById("link").value;
    webpage = "http://" + url;  
}

I have a question about anchor tags and javascript.Converting a URL to an anchor tag

The text box accepts a url (eg. "www.youtube.")

I made a Javascript function that adds "http://" to the link.

How do I make it so the convert button adds a link on the webpage that takes you to the website in another tab.

My Javascript code is as follows:

var webpage="";
var url="";
var message="";
var x= 0;
var page="";

function convert()
{       
    url=document.getElementById("link").value;
    webpage = "http://" + url;  
}
Share Improve this question edited Feb 18, 2017 at 21:23 RupamDotInfo 5712 bronze badges asked Feb 18, 2017 at 19:58 Kyle GoertzenKyle Goertzen 1431 gold badge2 silver badges8 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 8

You could generate the elements and apply the needed attributes to it. Then append the new link to the output paragraph.

function generate() {
    var a = document.createElement('a');
    
    a.href = 'http://' + document.getElementById('href').value;    
    a.target = '_blank';
    a.appendChild(document.createTextNode(document.getElementById('href').value));
    document.getElementById('link').appendChild(a);
    document.getElementById('link').appendChild(document.createElement('br'));
}
Link: <input id="href"> <button onclick="generate()">Generate</button>
<p id="link"></p>

You can just do this by adding an anchor tag dynamically

var mydiv = document.getElementById("myDiv");
var aTag = document.createElement('a');
aTag.setAttribute('href',webpage);
aTag.innerHTML = "link text";
mydiv.appendChild(aTag);

Please have a look here for more references

How to add anchor tags dynamically to a div in Javascript?

function addLink()
{       
    var url = document.getElementById("link").value;
    var webpage = "http://" + url;
    
    var a = document.createElement("a");                 // create an anchor element
    a.href = webpage;                                    // set its href
    a.textContent = url;                                 // set its text
    
    document.getElementById("container").appendChild(a); // append it to where you want
}
a {
  display: block;
}
<div id="container"></div>
<br><br>
<input id="link"/><button onclick='addLink()'>ADD</button>

I assume you know how to write JavaScript, so I will not go there. The issue is understanding the target attribute of an <a> tag.

W3Schools: Target Attribute

发布评论

评论列表(0)

  1. 暂无评论