如果按Ctrl + F,则禁用表单查找浏览器

时间:2017-03-14 14:58:27

标签: javascript jquery html css browser

如果按 Ctrl + F 并关注元素,如何禁用表单查找浏览器 的 HTML

<div id='demo'>
    <form class="id5-text-find-form" id="id5-text-find-form">
        <input class="search" placeholder="Find..." type="text">
        <input class="reset" type="reset" value="x">
    </form>
</div>


<textarea id="area">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.   
</textarea>

css:

#demo {
    display:none;
}
#area {
    width:100%;
    height:200px;
}
kbd {
    border:1px solid grey;
    padding:4px;
}

jQuery:

$(document).keydown(function(e) {
    if ( e.ctrlKey && ( e.which === 70 ) ){
        $("#demo").show();
    }
})
$(".reset").click(function() {
    $("#demo").hide();
})

图片:

enter image description here

Fiddle Demo :

1 个答案:

答案 0 :(得分:3)

在你的keydown函数中使用e.preventDefault()来防止默认行为

使用$(".search").focus();设置焦点

$(document).keydown(function(e) {
    if ( e.ctrlKey && ( e.which === 70 ) ){
        $("#demo").show();
        e.preventDefault();
        $(".search").focus();
    }
})

https://jsfiddle.net/ycgdd1gd/2/