Javascript ~ Button onkeypress don't do something~press enter do nothing

if you wish a button clicked by mouse ~~~do something

clicked by keyboard like~Enter of Space~~~~~~do nothing~

here is a simple way to solve~~XD


HTML Code
<button onclick="mouse_click()" onkeypress="keyboard_press()">sdf</button>

Javascript
-------------------------------------------------------------------------------------------
var keyboardClick = false;


onkeypress="keyboard_press()"
-------------------------------------------------------------------------------------------
function keyboard_press()
{
keyboardClick = true;
}


onclick="mouse_click()"
-------------------------------------------------------------------------------------------
function mouse_click()
{
if(keyboardClick == true)
{
alert('do nothing');
keyboardClick = false;
}
else
{
alert('do something');
}
}

====================================================================
if I use the event.keyCode~

CODE
onclick="mouse_click(event.which)"

function mouse_click(e)
{

if ( ! e )
{
e = event.keyCode;
}
}

if e is 13 or 32 nothing happened

it didn't work~

because SPACE(32) key or ENTER(13) key~

will triggered the onkeypress event then triggered the onclick event ~~

reference~
http://www.elated.com/articles/events-and-event-handlers/

http://plog.longwin.com.tw/files/key_codes.html

http://www.lslnet.com/linux/f/docs1/i45/big5314758.htm

沒有留言:

發佈留言