如何在hello函数内检测是否按住shift?<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
function hello(){
//如果按住了shift键,则alert("shift");
//如果纯单击,则alert("no shift");

//希望只通过现有html结构直接完成,不需要在添加id或class,不需要$("span").click(e,fn)获取e.shiftKey
}
</script>
<span onclick="hello()">hello</span>

解决方案 »

  1.   

    <script type="text/javascript">
    var shift = false;document.documentElement.onkeydown = function(e)
    {
    e = e || window.event
    if(e.keyCode == 16) shift = true
    }document.documentElement.onkeyup = function(e)
    {
    e = e || window.event;
    if(e.keyCode == 16) shift = false;
    }    function hello(){
            //如果按住了shift键,则alert("shift");
            //如果纯单击,则alert("no shift");
            
            //希望只通过现有html结构直接完成,不需要在添加id或class,不需要$("span").click(e,fn)获取e.shiftKey
    if(shift) alert("shift") 
    else alert("no shift")
        }
    </script>
    <span onclick="hello()">hello</span>