因此,由于需要解决iOS错误,因此我四处查看,我的模拟点击似乎都无效。我试图手动将注意力集中在textarea元素上,但是要做到这一点,我需要手动模拟对所述textarea元素的点击。所以这是我尝试过的:
$('#text-input')[0].click();
$('#text-input').click();
$('#text-input').trigger('click');
$('#text-input').focus();
和
$('#text-input').on('click', function {
$(this).focus();
});
$('#text-input').trigger('click');
$('#text-input').on('touchstart', function {
$(this).focus();
});
$('#text-input').trigger('touchstart');
HTML:
<textarea id="comment-input" class="comment-input" rows="1" maxlength="{{maxTextLength}}" ng-model="newMessage.content" placeholder="{{ messagePlaceholder }}" />
有什么想法为什么不起作用?
最佳答案
尝试使用.focus
而不是click
$('#text-input').trigger('focus');
要不就
$('#text-input').focus();
更新
您可以尝试在大多数情况下都可以使用的
setTimeout
$("#text-input").on("click", function(event) {
setTimeout(function() {
$(event.target).select(); //or $(event.target).focus();
}, 1);
});
更新2
您也可以尝试使用
touchstart
事件,例如//trigger touch on element to set focus
$("#text-input").trigger('touchstart');
//event handler to set the focus()
$('#text-input').on('touchstart', function () {
$(this).focus(); // inside this function the focus works
});