<a href="<?=$rowz[0]?>" onClick="countLinks('<?=$row[6]?>','<?=$indexl?>')">[Link <?=$j++?>]</a>
问题是在IE或Firefox浏览器上无法使用中间按钮。 实际上,只有在Chrome浏览器中才会调用使用中间按钮的
countLinks
函数。我认为我需要一个类似于
mouseup
事件的Jquery函数,但我不知道如何调用该函数,并使用这些参数调用countLinks
函数。有什么帮助吗?
<a href="<?=$rowz[0]?>" onClick="countLinks('<?=$row[6]?>','<?=$indexl?>')">[Link <?=$j++?>]</a>
countLinks
函数。mouseup
事件的Jquery函数,但我不知道如何调用该函数,并使用这些参数调用countLinks
函数。你说得对。您需要 mousedown
或 mouseup
事件来确定实际点击了哪个鼠标按钮。
但首先,您需要摆脱内联事件处理程序 onclick
并遵循 非侵入式 JavaScript 的光辉之路。
因此,您需要为该锚点添加 id
或 class
标记以标识它(当然,您也可以选择使用 css 选择器选择该锚点)。假设我们已经添加了一个名为 myClazzz 的类 :)
JavaScript:
$(function(){
$('.myClazzz').bind('mouseup', function(e){
switch(e.which){
case 1:
alert('Left Mouse button pressed.');
break;
case 2:
alert('Middle Mouse button pressed.');
break;
case 3:
alert('Right Mouse button pressed.');
break;
default:
alert('You have a strange Mouse!');
}
});
});
mousedown
/ mouseup
事件处理程序中,which
属性将包含一个数字,指示点击了哪个鼠标按钮。click
?您还可以查看已单击的按钮是左、中还是右...在跨浏览器上,mouseup
和 mousedown
的行为可能会有所不同。 - Sinanclick
事件结合了mousedown
+ mouseup
,仅在左键点击时触发。 - jAndyhtml5
属性...实际上在html5之前也有可能实现,但它没有通过验证。data
属性中。 然后像这样编写js代码:$(function(){
//use mouseup, then it doesn't matter which button
//is clicked it will just fire the function
$('.myClazzz').bind('mouseup', function(e){
//get your params from data attributes
var row = $(this).attr("data-row"),
index = $(this).attr("data-index");
//and fire the function upon click
countLinks(row,index);
});
});
//don't forget to include jquery, before these lines;)
$(this).data('row')
与$(this).attr('data-row')
是相同的。 - WildlyInaccurate$( document ).click(
function ( evt ) {
// ... evt.which === 2 means middle click
}
);
这有点像一个技巧(通常你会在链接本身上监听事件),但它是有效的。