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?
-
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
3 Answers
Reset to default 6You 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
版权声明:本文标题:jquery - Add Link Element <a> to paragraph <p> Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741925326a2405249.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论