简体   繁体   English

JS单击功能在新窗口中打开URL

[英]JS click function open url in new windown

I create a link with text using html and i want to create when user click it goes to specific url. 我使用html创建带有文本的链接,我想在用户单击它时转到特定的URL。 I new in javascript!! 我是javascript新手!

here my code 这是我的代码

<a class="dt-button add_new_table_entry DTTT_button DTTT_button_new" tabindex="0" aria-controls="table_1" href="#">
<span>Add new</span>
</a>

my script 我的剧本

$("dt-button add_new_table_entry DTTT_button DTTT_button_new").click(function () {
window.location="http://project.co/add-new/";
});

UPDATE I have create my script 更新我已经创建了我的脚本

 let classic = document.getElementsByClassName('add_new DTTT_button DTTT_button_new');
for (var i=0; i < classic.length; i++) {
classic[i].addEventListener("click", clickfunc, false);
}
function.clickfunc(){
document.location.href="http://google.com";
}

but give error unexpected token ; 但是给错误意外令牌;

javascript打开新窗口

window.open(url, windowName, "height=200,width=200");

You do not have any class named add_new . 您没有任何名为add_new类。 Using single unique class in getElementsByClassName parameter is enough. getElementsByClassName参数中使用单个唯一的类就足够了。 Try the following: 请尝试以下操作:

let classic = document.getElementsByClassName('dt-button');
for (var i=0; i < classic.length; i++) {
  classic[i].addEventListener("click", clickfunc, false);
}
function clickfunc(){
  window.open("http://google.com","_blank");
}

use follwing code 使用以下代码

$("dt-button add_new_table_entry DTTT_button 
DTTT_button_new").click(function () {
window.open("http://project.co/add-new/");
});

create a function : 创建一个函数:

function openlink()
{
window.open("http://project.co/add-new/","mywindow","menubar=no,resizable=1,width=400,height=400");
}

then you call the function using this : 然后使用以下命令调用该函数:

<a href="javascript:openlink();">Link Text</a>

Try it , it will work, here on click of class = add_new_table_entry, javascript click event gets called and in same window redirection happens we use window.location for redirection 尝试一下,它将起作用,在单击class = add_new_table_entry时,将调用javascript click事件,并且在同一窗口重定向中,我们使用window.location进行重定向
Add new 添新

$(".add_new_table_entry").click(function () {
        window.location ="http://project.co/add-new/";
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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