jQuery创建输入字段并设置焦点 [英] jQuery create input field and set focus

查看:72
本文介绍了jQuery创建输入字段并设置焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试:

  • 使用输入字段创建新的列表项
  • 将焦点设置到该新字段

      $(#newList").click(function(){$('li.divider').before('< li class ="newListName">< input id ="newListNameInput"/></li>',function(){$('#newListNameInput').focus();});}); 

新列表项被创建,但是没有焦点.非常感谢您的帮助!

解决方案

没有任何处理程序作为 before 的第二个参数,因此只需使用:

  $(#newList").click(function(){$('li.divider').before('< li class ="newListName">< input id ="newListNameInput"/></li&';));$('#newListNameInput').focus();}); 

但是,如果添加许多新的 input 元素,请注意ID的重复.也许最好使用类.

演示: http://jsfiddle.net/CANWE/

I'm trying to:

  • create a new list item with an input field
  • set the focus to that new field

    $("#newList").click(function () {
        $('li.divider').before('<li class="newListName"><input id="newListNameInput" /></li>',function() {
            $('#newListNameInput').focus();
        });
    });
    

The new list item gets created, but no focus. Help is much appreciated!

解决方案

There is no handler as a second parameter of before, so just use:

$("#newList").click(function() {
    $('li.divider').before('<li class="newListName"><input id="newListNameInput" /></li>');
    $('#newListNameInput').focus();
});

But be careful with duplication of IDs if you add many new input elements. Maybe it is better to use classes.

DEMO: http://jsfiddle.net/CANWE/

这篇关于jQuery创建输入字段并设置焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆