2014-01-16 67 views
1

下面的代码设置了我的树(s:property标签struts2的东西):jstree:左/中鼠标区分点击

$(function() { 
     $("#networkTree").jstree({ 
      "json_data" : { 
      "ajax" : { 
       "url" : "<s:property value='networkTreeDataUrl'/>" 
      } 
      }, 
      "plugins" : [ "themes", "json_data", "ui" ], 
      "themes" : { 
       "theme" : "default", 
       "dots" : true, 
       "icons" : false 
      }, 
      "core" : { 
       "html_titles" : true 
      } 
     }).bind("select_node.jstree", function (event, data) { 
       window.location.href = "<s:property value='companyDetailsUrl'/>" + "?companyId=" + data.rslt.obj.attr("id"); 
       }) 
}); 

当用户左点击取决于窗口的URL变化的树项目companyDetailsUrl。到目前为止正确,但我希望浏览器(铬)打开链接在一个新的标签,当我点击中间的鼠标按钮照常。看起来任何鼠标点击选择树节点,这触发了替代window.location的绑定事件。什么是防止这种情况的最好方法?

回答

2

我会为which-属性的事件处理程序。
这提供了一种简单的方法来区分按钮,根据jQuery的文档:

event.which也归按钮按压(鼠标按下和mouseupevents),报告1左按钮,2中间,3为右。

//rest of the code omitted 
.bind("select_node.jstree", function (event, data) { 
    if(event.which == 1) { 
     //open link in current window 
     window.location.href = YOURURL; 
    } else if (event.which == 3) { 
     //open link in new window 
     window.open(YOURURL, '_blank'); 
    } 
}) 

请注意,您必须更换YOURURL(明显)。为了便于阅读,我省略了它。

另外请注意,这很可能会打开一个新的窗口,而不是新的标签。要进一步阅读为什么它会打开一个新窗口以及如何打开新标签,我建议您阅读this question