Open Page in New Tab Using JavaScript

2019-09-20 12:54发布

I can't figure out how to open a new tab using JavaScript. I went through related questions but nothing worked for me. I want the browser to open a new tab when someone clicks on the link (example.com) below. What code should I add and where should I place that code?

My html code is here -

<section id="work-grid" class="site-inner">
<div class="work-item" data-groups='["all", "webdesign"]' data-url="http://example.com">
<figure class="image-container"><img src="images/work/web-one.jpg" /></figure>
</div>

Thank you in advance.

2条回答
ゆ 、 Hurt°
2楼-- · 2019-09-20 13:41

Use the target attribute which specifies where to open the linked document,

<a href="http://example.com" target="_blank">Example</a>

or in javascript,

function myFunction() {
    // Open the link in new browser window.
    window.open("http://example.com");
}
查看更多
疯言疯语
3楼-- · 2019-09-20 13:44

I can't see element from code HTML. and data-groups='["all", "webdesign"]' data-url="http://example.com" from #work-item using for?

From HTML Code.

<section id="work-grid" class="site-inner">
<div class="work-item">
<figure class="image-container"><img src="images/work/web-one.jpg" onclick="openNewTabLink('example.com')"/></figure>
</div>

From Javascript Code.

function openNewTabLink(link){window.open(link,'_blank');}
查看更多
登录 后发表回答