我目前正在设计一个简单的论坛应用程序.它主要由jQuery/AJAX提供支持,并在同一页面上加载所有内容; 但是,我知道有时用户想要在浏览论坛时一次打开几个主题以在新标签中查看它们.
我的解决方案是检测何时单击鼠标中键并单击鼠标左键,然后执行不同的操作.我想在单击鼠标左键时在窗口中加载AJAX目标,否则将其加载到新选项卡中.
我唯一的问题是我不知道用jQuery在新选项卡中打开目标位置的方法.显然,不允许随意打开新选项卡,但是有没有办法将这种类型的行为分配给用户生成的操作?
谢谢!
Nim*_*ani 16
请查看示例代码.它可能有所帮助
<script type='text/javascript'>
jQuery(function($){
$('a').mousedown(function(event) {
switch (event.which) {
case 1:
//alert('Left mouse button pressed');
$(this).attr('target','_self');
break;
case 2:
//alert('Middle mouse button pressed');
$(this).attr('target','_blank');
break;
case 3:
//alert('Right mouse button pressed');
$(this).attr('target','_blank');
break;
default:
//alert('You have a strange mouse');
$(this).attr('target','_self"');
}
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
<a href="some url" target="_newtab">content of the anchor</a>
Run Code Online (Sandbox Code Playgroud)
在javascript中你可以使用
$('#element').mousedown(function(event) {
if(event.which == 3) { // right click
window.open('page.html','_newtab');
}
})
Run Code Online (Sandbox Code Playgroud)
您还需要考虑使用ctrl-click和cmd-click来打开新选项卡(分别在windows/linux和mac中.因此,这将是一个更完整的解决方案:
jQuery.isClickEventRequestingNewTab = function(clickEvent) {
return clickEvent.metaKey || clickEvent.ctrlKey || clickEvent.which === 2;
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
25908 次 |
| 最近记录: |