admin管理员组文章数量:1414874
I have the following html code:
<div class="someclass">
<ul>
<li><a title="someFile" href="somefolder/somefile.pdf">File Name</a>
<li><a title="someFile2" href="somefolder/somefile2.pdf">File 2</a>
</ul>
</div>
I would like the link to bee:
<li><a title="someFile" href="somefolder/somefile.pdf" target="_blank">File Name</a>
<li><a title="someFile2" href="somefolder/somefile2.pdf" target="_blank">File 2</a>
Can this to be done with pure javascript, without jquery or other libraries?
I have done similar things with jquery using getElementByID, but here element has no id, all that it has is the class "someclass" and for various reasons, I don't want to change the html. What I am trying to do is just to insert small javascript on the bottom of the page which will be executed when the document is loaded using the:
window.onload="myFunction()";
Can this task be acplished on the way that I want to do it?
I have the following html code:
<div class="someclass">
<ul>
<li><a title="someFile" href="somefolder/somefile.pdf">File Name</a>
<li><a title="someFile2" href="somefolder/somefile2.pdf">File 2</a>
</ul>
</div>
I would like the link to bee:
<li><a title="someFile" href="somefolder/somefile.pdf" target="_blank">File Name</a>
<li><a title="someFile2" href="somefolder/somefile2.pdf" target="_blank">File 2</a>
Can this to be done with pure javascript, without jquery or other libraries?
I have done similar things with jquery using getElementByID, but here element has no id, all that it has is the class "someclass" and for various reasons, I don't want to change the html. What I am trying to do is just to insert small javascript on the bottom of the page which will be executed when the document is loaded using the:
window.onload="myFunction()";
Can this task be acplished on the way that I want to do it?
Share Improve this question asked Nov 5, 2015 at 9:24 user2417624user2417624 6931 gold badge10 silver badges33 bronze badges 2- jQuery is written in Javascript. So everything jQuery can do, can be done with Javascript. – Ivar Commented Nov 5, 2015 at 9:27
- As i understand the difference only in target attribute? – The Reason Commented Nov 5, 2015 at 9:28
5 Answers
Reset to default 4you just need to write
var matches = document.querySelectorAll('a');
for (var i = 0; i < matches.length; i++) {
matches[i].setAttribute('target', '_blank');
}
Fiddle
var links = document.querySelector('.someclass').getElementsByTagName('a');
Array.prototype.forEach.call(links, function(el) {
// set attribute
el.setAttribute('target','_blank');
});
See http://jsfiddle/sjmcpherso/krktmghd/ for working example
This answer is is IE9+ so yes you don't really need jQuery
Try this:
var pageLinks = document.getElementsByTagName("a");
for(var x in pageLinks) {
pageLinks[x].target = "_blank";
}
Try this:
var prnt = document.querySelector('.someclass ul').childNodes;
for(var i=0;i<prnt.length;i++){
if(prnt[i].nodeType == 1){
prnt[i].firstChild.setAttribute('target','_blank');
}
}
Output:
<div class="someclass">
<ul>
<li><a title="someFile" href="somefolder/somefile.pdf" target="_blank">File Name</a>
<li><a title="someFile2" href="somefolder/somefile2.pdf" target="_blank">File 2</a>
</ul>
</div>
Working Demo
Have a look here.
http://www.w3schools./js/js_htmldom_nodes.asp
As indicated, this is what you can do with only Javascript
var para = document.createElement("p"); //create p element
var node = document.createTextNode("This is new."); //create text element
para.appendChild(node); //append text to p
var element = document.getElementById("div1");
element.appendChild(para); //append p to somewhere in your page
本文标签: how can I add an html to an element without id with javascript without jqueryStack Overflow
版权声明:本文标题:how can I add an html to an element without id with javascript without jquery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745216920a2648214.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论