gpt4 book ai didi

javascript - 使用 jquery 聚焦一个字段

转载 作者:行者123 更新时间:2023-11-30 10:30:30 25 4
gpt4 key购买 nike

我想知道如何才能专注于这个领域。这是我的代码: http://jsfiddle.net/spadez/9sX6X/31/

var container = $('.copies'),
value_src = $('#current'),
maxFields = 10,
currentFields = 1;

$('.copy_form').on('click', '.add', function () {
if ($.trim(value_src.val()) != '') {
if (currentFields < maxFields) {
var value = value_src.val();
var html = '<div class="line">' +
'<input id="accepted" type="text" value="' + value + '" />' +
'<input type="button" value="X" class="remove" />' +
'</div>';

$(html).appendTo(container);
value_src.val('');
currentFields++;
} else {
alert("You tried to add a field when there are already " + maxFields);
}
} else {
alert("You didn't enter anything");
}
})
.on('click', '.remove', function () {
$(this).parents('.line').remove();
currentFields--;
});

当我在“内容”中添加一个值并单击“添加”时,我希望它重新关注“当前”字段,以便我可以开始输入更多内容。

最佳答案

您需要 focus() jQuery 函数:

.focus()

Bind an event handler to the "focus" JavaScript event, or trigger that event on an element.

This method is a shortcut for .on('focus', handler) in the first and second variations, and .trigger('focus') in the third.

The focus event is sent to an element when it gains focus. This event is implicitly applicable to a limited set of elements, such as form elements (, , etc.) and links (). In recent browser versions, the event can be extended to include all element types by explicitly setting the element's tabindex property. An element can gain focus via keyboard commands, such as the Tab key, or by mouse clicks on the element.

Elements with focus are usually highlighted in some way by the browser, for example with a dotted line surrounding the element. The focus is used to determine which element is the first to receive keyboard-related events


因此,在您的代码中只需添加:

 $("#current").focus();

JSFIDDLE

关于javascript - 使用 jquery 聚焦一个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17193136/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com