【问题标题】:JS click function open url in new windownJS点击功能在新窗口中打开网址
【发布时间】:2018-04-25 16:27:39
【问题描述】:

我使用 html 创建了一个带有文本的链接,并且我想在用户单击它时创建它转到特定的 url。我是javascript新手!!

这是我的代码

<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>

我的脚本

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

更新 我已经创建了我的脚本

 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";
}

但给出错误意外令牌;

【问题讨论】:

    标签: javascript jquery url hyperlink


    【解决方案1】:

    javascript 打开新窗口

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

    【讨论】:

      【解决方案2】:

      您没有任何名为add_new 的类。在getElementsByClassName 参数中使用单个唯一类就足够了。请尝试以下操作:

      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");
      }
      

      【讨论】:

      • @Ibrahim Quraisy,我已经更新了我的答案。请看一看。
      【解决方案3】:

      使用以下代码

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

      【讨论】:

        【解决方案4】:

        创建一个函数:

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

        然后你用这个来调用函数:

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

        【讨论】:

          【解决方案5】:

          试试吧,它会起作用,在这里点击 class= add_new_table_entry,javascript 点击事件被调用并在同一个窗口重定向发生 我们使用 window.location 进行重定向
          添新

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

          【讨论】:

            猜你喜欢
            • 2019-05-06
            • 2013-12-28
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-11-30
            • 1970-01-01
            • 2019-06-21
            相关资源
            最近更新 更多