2017-06-13 217 views
0

我有編輯操作按鈕,沒有選項「打開新選項卡」時,我右鍵單擊。 這裏是我的代碼:瀏覽器選項在新選項卡中打開鏈接

function actionsTemplateConversion(data) { 
    if (data != null){ 
     if (canEdit){ 
       row += '<a id="edit" title="${editTr}" onclick="edit(event);"></a>'; 
      } 
      return row; 
     } 
    } 

function edit(event) { 
    table = $('#table').data(); 
    var dataItem = table.dataItem($(event.currentTarget).closest("tr")); 

    window.location = "users/"+dataItem.id+"/editUser.html?id="+"${id}"+"&name="+"${name}"; 
    } 

我已經嘗試使用HREF代替的onclick,並把這個鏈接從編輯()函數href和與我得到的選項打開在右鍵點擊新標籤,但也有這個問題,因爲如果有一個單一的名稱,而不是因爲這個qoutes而引發的所有事情。所以唯一的辦法就是使用這個功能。 我也試過用這個window.location.href但它不工作。 有沒有其他選項可以在window.location旁邊打開鏈接?

+0

[瀏覽器,JavaScript,以便在新標籤window.open(可能的重複https://stackoverflow.com/questions/15818892/chrome-javascript-window-open -in-new-tab) –

+0

我看到了這個,但它不能幫助我,因爲這個解決方案總是在新標籤中打開鏈接。我需要這個選項,以便用戶可以選擇他在哪裏編輯數據 – alonso05

回答

0

你可以在錨標記瞄準這一事業的瀏覽器打開新的標籤頁<a target="_blank" href=""/>

OR

if(user) 
window.open(encodeURI('url'), '_blank'); 
else 
window.open(encodeURI('url')); 

如果名稱報價試圖對其進行編碼 https://www.w3schools.com/TAGS/ref_urlencode.asp

+0

這將始終在標籤中打開,但我需要獲得此選項,以便用戶可以選擇他在哪裏編輯數據 – alonso05

+0

@ alonso05查看更新回答 –

+0

什麼是(用戶)在這個如果條件 – alonso05

0
<a id="edit" title="${editTr}" onclick="edit(event);"></a> 

這不是一個鏈接。鏈接必須具有href屬性。沒有一個,當你點擊它時沒有URL打開,或者在新標籤頁中打開。只有一個JavaScript程序可以運行。

我試着使用的href代替的onclick

這就是解決方案從編輯

鏈接()函數href和與我得到的選項打開在右鍵點擊新標籤,但也有問題,因爲如果有單一qoutes在名稱比所有meesses因爲該qoutes

這是通過將字符串混合在一起而不是使用DOM來生成HTML的問題。

你必須非常小心地逃避一切。

改爲使用DOM。

由於您已經在使用jQuery了。沿着這些線路的東西應該讓你開始:

var url = "users/" + encodeURIComponent(dataItem.id) + "/editUser.html?id=" + encodeURIComponent("${id}") + "&name=" + encodeURIComponent("${name}"); 
var $link = jQuery("<a/>") 
      .attr("href", url) 
      .attr("title", "${editTr}") 
      .attr("id", "edit") // Are you sure this is a UNIQUE id? Should this be a class instead? 
      .on("click", edit); 
相關問題