JS 键盘回车和鼠标单击事件合并

JavaScript08

JS 键盘回车和鼠标单击事件合并,第1张

一个是键盘事件,一个是mouse 事件!

是不可以合并的!

不就是执行重复的么?

重新定义个函数啊!repeat=function(){$('#single_slide_show_wrapper').addClass('slideshow_already_start')

$("video").prop('muted', true)// if is home page

$('.slider_wrapper').unslider({

speed: 500,

// The speed to animate each slide (in milliseconds)

delay: false,

arrows: true,

fluid: true,

keys: true,

dots: true,

complete: function() {}}

直接调用就可以了呀!

触发keydown事件需要控件获取到焦点,div控件无法像input控件那样获取到焦点,无法触发keydown事件,因此可以将keydown事件绑定在document上,然后用div的hover事件去判断鼠标是否正在div上。

<!DOCTYPE html>

<!--STATUS OK-->

<html>

<head>

    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />

    <meta http-equiv="content-type" content="text/htmlcharset=utf-8" />

    <meta property="wb:webmaster" content="3aababe5ed22e23c" />

    <meta name="referrer" content="always" />

    <title>s鼠标悬浮事件嵌套键盘事件问题,点击空格并没有响应,求大神来帮助!_百度知道</title>

    <style>

        .divTest { width: 200px height: 200px border: 1px solid #CCC }

    </style>

    <script src="http://cdn.static.runoob.com/libs/jquery/1.10.2/jquery.min.js"></script>

    <script>

        var divFocus = false

        $(function () {

            $(".divTest").hover(function () {

                divFocus = true

                $(".divTest").text(2)

            }, function () {

                divFocus = false

                $(".divTest").text("")

            })

            $(document).keydown(function (event) {

                if (!divFocus) {

                    return

                }

                if (event.keyCode != 32) {

                    return

                }

                $(".divTest").text(101)

            })

        })

    </script>

</head>

<body>

    <div class="divTest"></div>

</body>

</html>