V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
fancy2020
V2EX  ›  程序员

被一个 iOS Safari 上的问题恶心到了

  •  
  •   fancy2020 · Jul 6, 2020 · 2095 views
    This topic created in 2132 days ago, the information mentioned may be changed or developed.

    首先,在页面上有一个 input,然后在 js 中动态的改变 input 的 readOnly 属性,比如点击 input 的时候取消 readonly 属性,onblur 的时候加上 readonly 使它变为只读的。

    比如如下 React 代码:

    1 import React, { useState, useRef } from 'react';
     2 import logo from './logo.svg';
     3 import './App.css';
     4
     5 function App() {
     6   const [editable, setEditable] = useState(true);
     7   const ref = useRef();
     8
     9   function handleClick() {
    10   ┆ setEditable(true);
    11   }
    12
    13   function handleBlur() {
    14   ┆ setEditable(false);
    15   }
    16
    17   return (
    18   ┆ <div className="App">
    19   ┆ ┆ <input
    20   ┆ ┆ ┆ ref={ref}
    21   ┆ ┆ ┆ onClick={handleClick}
    22   ┆ ┆ ┆ onBlur={handleBlur}
    23   ┆ ┆ ┆ readOnly={!editable}
    24   ┆ ┆ />
    25   ┆ </div>
    26   );
    27 }
    28
    29 export default App;
    

    然后在 iOS Safari 上的表现是,当 readOnly 由 false 变为 true 的时候,input 中有光标但是无法唤起虚拟键盘。 在网上搜了一圈,貌似也没找到太多相关资料

    5 replies    2020-07-07 17:03:23 +08:00
    KuroNekoFan
        2
    KuroNekoFan  
       Jul 7, 2020
    scroll 一下,focus 一下试试
    holybig
        3
    holybig  
       Jul 7, 2020
    onClick 改 onFocus
    fancy2020
        4
    fancy2020  
    OP
       Jul 7, 2020
    1 楼的方案可以解决,谢谢大家
    xingyuc
        5
    xingyuc  
       Jul 7, 2020
    setTimeout 试试
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1114 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 18:25 · PVG 02:25 · LAX 11:25 · JFK 14:25
    ♥ Do have faith in what you're doing.