开发者

from HTML send an event plus variable to JS function

I want to send a mouse event and a variable to a JS function from within my html code, i can send the event fine but the proble开发者_StackOverflow社区m is sending the event AND a variable. I have included a stripped down version of what I am trying to do. The browser is chrome. Can anyone offer some advice please ??`

function mouseDown(e,w) {
var ctrlPressed=0;
var evt = navigator.appName=="Netscape" ? e:event;

ctrlPressed =evt.ctrlKey;
self.status="" +"ctrlKey=" +ctrlPressed 

    if (ctrlPressed) 
    alert ("Mouse clicked with Ctrl/n and the variable is " + w )
    return true;
}

</script>
<body>

<table border = "1" >
<tr> 
<td onmousedown="mouseDown(e,\"variable\")"> Link 1 </td>
<td> Link 2 </td> 
</tr> 
</table> 
</body>`


Don't use browser detection. Check whether an object exists instead.

var evt = navigator.appName=="Netscape" ? e:event; // wrong
var evt = e ? e : event;    // right
var evt = e || event;       // also right

Since the event is passed as the 1st argument, you could try

<td onmousedown="mouseDown(arguments[0], 'variable');"> Link 1 </td>

(I have only tested this on Safari, not guarantee to work on other browsers.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜