手动触发关注输入Iphone问题



单击另一个元素后,我试图将焦点集中在文本区域。桌面浏览器似乎运行良好,但Iphone却不行。

$('.reveal_below').on('change', function(e) {
    e.preventDefault();
    var item_id = $(this).attr('data-item-id');
    if (this.checked) {
        $('.hidden_below__' + item_id).css({
                        'margin-left': '0px',
                        display: 'block',
                        opacity: '0'
                    }).animate({ 
                       'margin-left': '15px',
                        opacity: '1'
        }, 
 250, function() {
    console.log("-----------");
    $("#x").focus();
})
    } else {
        $('.hidden_below__' + item_id).slideUp();
    }
});

这是一个小小的演示

它将把文本区域集中在复选框的更改事件上。这就是问题所在,我如何用演示中的动画来解决这个问题?

您必须使用touchstart事件来解决此问题。

$(document).ready(function() {
  $('button').on('click', function() {
    $('#x').css({
      'margin-top': '0px',
      display: 'block',
      opacity: '0'
    }).animate({
        'margin-top': '15px',
        opacity: '1'
      },
      100
    )
    $('#x').trigger('touchstart'); //trigger touchstart
  });
  $('textarea').on('touchstart', function() {
    $(this).focus();
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<textarea id="x" style="width: 100%;height:6em;display: none" placeholder="Return notes..." cols="30" rows="10"></textarea>
<button type="button">focus textarea</button>

最新更新