jquery - 手动触发焦点输入 Iphone 问题

标签 jquery ios iphone onfocus

我试图在点击另一个元素后聚焦文本区域。桌面浏览器似乎工作正常但不是 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();
    }
});

这里有点demo

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

最佳答案

你必须使用 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>

关于jquery - 手动触发焦点输入 Iphone 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31040611/

相关文章:

iphone - 在 UITextField 中显示标记

iphone - 使用 NSFileManager 将文件复制/移动到路径,自动创建中间目录

javascript - 编写一个函数,该函数接受一个对象数组以及一个具有属性和值的对象

jquery - 标题按钮悬停颜色

javascript - "Uncaught Error: Syntax error, unrecognized expression:"具有自动完成功能(特殊字符?)

ios - 如何在苹果开发者页面添加我的 iOS 设备?

ios - Swift - 根据日期数组对不同的数组进行排序

php - 类型错误 : non-native scope object

iphone - 为 NSTimer 添加暂停功能

ios - 从 UIAutomation 调用 webservice