如果用户键入任何内容,如何使用jquery .focus()元素
How to jquery .focus() element if user types anythign
我在父元素li上嵌套了一个鼠标悬停激活的ul。
所有的工作,除了我有一个搜索表单在飞出菜单,我希望这个输入字段集中,如果用户键入任何。
我好像搞不懂这部分,有什么主意吗?我现在是如何实现的,只有当我已经选择input并按下某个键时才有效,这是因为$this是input。
//Flyout menu in big_header
$(".flyout_big").removeClass("fallback");
$("#head_big .categories > ul > li").hover(
function() {
//$('ul', this).stop().slideDown(100);
$(".flyout_big", this).stop().fadeIn("fast");
var offset = $(this).offset();
$(".flyout_big .flyout_arrow", this).stop().animate({top:offset.top},"slow");
//Activate-focus search field on any key-press
$(document).on("keydown",function(e){
//if (e.keyCode == 13) {
// alert("enter");
//} else if (e.keyCode == 27) {
// alert("esc");
//}
//alert("test");
$(".search_field").focus();
});
},
function() {
//$('ul', this).stop().slideUp(100);
$(".flyout_big", this).stop().fadeOut("fast");
});
//End floyout menu in big_header
Html:
<ul>
<li>Test 1<br /><input type="text" /></li>
<li>Test 2</li>
<ul>
脚本:
$(document).ready(function()
{
$("ul > li").hover(function()
{
$("ul > li").removeClass('active');
$(this).addClass('active');
});
$('html').on("keyup",function()
{
alert('test');
$("ul > li").filter(".active").children('input').val('test').focus();
});
});
jsFiddle(注意你首先必须在html框上点击一次):
http://jsfiddle.net/6hEwD/使用此代码:
$('li *').hide();
$("ul > li").hover(function(){
var $this = $(this);
$this.find('*').show();
$(document).on("keydown",function(){
$this.children('input').focus();
});
},
function(){
var $this = $(this);
$this.find('*').hide();
});
小提琴:http://jsfiddle.net/6hEwD/4/
当文档处于焦点位置并且你将div悬停时,你可以在input中输入
相关文章:
- 我可以在Javascript/jQuery中使用一个变量作为键吗
- 如何在jquery中使用实时计算求和值
- 在jQuery中使用$('body').html('')意味着什么
- 在Jquery中使用变量作为对象名称
- 如何将while循环中生成的值存储到变量中,以便以后在JQuery中使用
- jquery无法使用无限滚动
- 将FormParams与Jersey和jQuery一起使用
- jQuery UI使用json文件自动完成
- SlideDown()不会'不能在jQuery中使用After()
- 在JQuery中使用谷歌地图Api事件/侦听器
- jQuery字符串使用split()方法在空格后拆分字符串
- 自动完成jquery;t使用ngmodel和ngrepeat(angular)
- 在jquery中使用css获取背景值
- jQuery idTabs-使用鼠标悬停自动更改和淡入淡出
- Javascript:在jQuery中使用xpath
- jQuery 无法使用 AJAX 调用访问函数内部的函数参数
- 在 for 循环 jQuery 中使用 one()
- Jquery/Javascript使用IF/ELSE语句更改img SRC
- jQuery动态使用.append()添加输入,但发布表单不是追加数据
- 存储数据的最佳方式是什么,以便与jquery一起使用