简体   繁体   中英

How to add target=blank in js?

Screenshot:

... http://freenetph.yn.lt/other/Screenshot_2017-06-25-14-06-44-1.png

I found this code for a random links on the 3rd party site (I forgot the name) and it looks so helpful to me but my problem is that, I want all those links will open in a new tab when it clicked. Anybody can help me? Im new to web development. Thanks in advance.

根据屏幕快照中的代码,尽管不建议使用document.write来执行此操作:

document.write('<a href="' + data[0] + '" target="_blank">' + data[1] + '</a>');
<script>
    window.open('http://link.com', '_blank');
</script>

As you are new to development, Instead of putting single line solution, I have added Code in your snippet, please check solution below.

 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script type="text/javascript"> var links = ["random/end1.html|Castle 1", "random/end2.html|Castle 2", "random/end3.html|Castle 3"]; function init() { var r = Math.floor(Math.random() * links.length), data = links[r].split('|'); var answer = document.getElementById('answer'); //document.write("<a href=\\""+data[0]+ "\\" target=\\"_blank\\">" + data[1] + "</a>"); answer.innerHTML = "<a href=\\"" + data[0] + "\\" target=\\"_blank\\">" + data[1] + "</a>"; }; </script> </head> <body onload="init()"> <div id="answer"></div> </body> </html> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM