admin管理员组

文章数量:1313175

I am trying to add a link into a paragraph that already has some text in it, but when I try this is just adds the text of the link element and not actual link. href or <a> tag. Here is html

<div id="main_div"></div>

And here is Javascript

var temp_link = document.createElement("A");
temp_link.href = "";
temp_link.target = '_blank';

var par = document.createElement("P");
par.innerHTML = "some text: " + temp_link;

<!--I have also tried this line below -->
<!--    par.insertAdjacentHTML('beforeend', '<div><a>' + url +'</a></div>' -->);

document.getElementById("main_div").appendChild(par);

I have tried two things what is in the above javascript both what it would currently run and the line that is mented out, neither attach the link, they just add the text I have included JSFiddle.

How can I add it so that <a> links?

I am trying to add a link into a paragraph that already has some text in it, but when I try this is just adds the text of the link element and not actual link. href or <a> tag. Here is html

<div id="main_div"></div>

And here is Javascript

var temp_link = document.createElement("A");
temp_link.href = "http://test.";
temp_link.target = '_blank';

var par = document.createElement("P");
par.innerHTML = "some text: " + temp_link;

<!--I have also tried this line below -->
<!--    par.insertAdjacentHTML('beforeend', '<div><a>' + url +'</a></div>' -->);

document.getElementById("main_div").appendChild(par);

I have tried two things what is in the above javascript both what it would currently run and the line that is mented out, neither attach the link, they just add the text I have included JSFiddle.

How can I add it so that <a> links?

Share Improve this question edited Dec 13, 2015 at 20:27 Mr Lister 46.6k15 gold badges113 silver badges155 bronze badges asked Dec 8, 2015 at 2:18 spen123spen123 3,52411 gold badges40 silver badges56 bronze badges 3
  • Why have you tagged the question with jQuery when you're not using it? – Lee Taylor Commented Dec 8, 2015 at 2:20
  • @LeeTaylor Im not using to it but an open to a solution that involves/ uses it – spen123 Commented Dec 8, 2015 at 2:21
  • You can use $("#main_div").append(par); to append the element. Also using $(*selector*).attr("attr name", "value") is probably your best bet for adding attributes. – CalebB Commented Dec 8, 2015 at 2:23
Add a ment  | 

3 Answers 3

Reset to default 6

You can not mix innerHTML and createElement. You need to append the element to the paragraph.

var temp_link = document.createElement("a");
temp_link.href = "http://test.";
temp_link.target = '_blank';
temp_link.innerHTML = "link";


var par = document.createElement("p");
par.innerHTML = "some text: ";
par.appendChild(temp_link);

document.getElementById("main_div").appendChild(par);
<div id="main_div"></div>

or

var temp_link = document.createElement("a");
temp_link.href = "http://test.";
temp_link.target = '_blank';
temp_link.innerHTML = "link";

var text = document.createTextNode("some text: ");

var par = document.createElement("p");
par.appendChild(text);
par.appendChild(temp_link);

document.getElementById("main_div").appendChild(par);
<div id="main_div"></div>

use innerHTML for the link name and outerhtml to get the full html of a elelmt

var temp_link = document.createElement("A");    
temp_link.href = "http://test.";
temp_link.target = '_blank';
temp_link.innerHTML ='click here';

var par = document.createElement("P");
par.innerHTML = "some text: " + temp_link.outerHTML;     

document.getElementById("main_div").appendChild(par);

Something like this should get you started:

var $p = $("<p>").text("some text:");
$p.append( 
    $("<a>").attr("href", "http://test.")
    .attr("target", "_blank")
    .text("click me") 
);
          
$("#main_div").append( $p);
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<div id="main_div"></div>

本文标签: jqueryAdd Link Element ltagt to paragraph ltpgt JavascriptStack Overflow