ChatGPT解决这个技术问题 Extra ChatGPT

使用 jQuery 选择焦点文本在 Safari 和 Chrome 中不起作用

我有以下 jQuery 代码(类似于 this question)在 Firefox 和 IE 中工作,但在 Chrome 和 Safari 中失败(没有错误,只是不工作)。任何解决方法的想法?

$("#souper_fancy").focus(function() { $(this).select() });
我想要 iPad/iPhone Safari 中的确切行为。这在 iPod/iPhone 浏览器中不起作用。任何线索。以下接受的答案仅适用于基于桌面的 Chrome/safari。
注意:这里接受的答案只能解决一半的问题。它使选择工作,但很难在随后的点击中取消选择它。可以在此处找到更好的解决方案:stackoverflow.com/questions/3380458/…

佚名

这是导致选择被取消选择的 onmouseup 事件,因此您只需要添加:

$("#souper_fancy").mouseup(function(e){
    e.preventDefault();
});

更多关于此处错误的参考:code.google.com/p/chromium/issues/detail?id=4505
如何使用原型实现相同的功能?
您还可以尝试绑定到 'click' 事件并避免进行两次绑定。
@uglymunky 根据您的操作,绑定到 click 事件并非在所有情况下都有效 - 毕竟,除了单击它之外,还有其他选择输入字段的方法,并且您希望这些方法也能正常工作(例如,进入它)
我想要 iPad/iPhone Safari 中的确切行为。这在 iPod/iPhone 浏览器中不起作用。任何线索。
J
Jason Sturges
$('#randomfield').focus(function(event) {
    setTimeout(function() {$('#randomfield').select();}, 0);
});

如果您尝试在 Android 上运行的 PhoneGap 应用程序的表单字段中选择文本,这是最佳答案。这向用户提供了文本被选中的视觉指示,而接受的答案没有。
J
Joe Chung

这适用于 input type="text" 元素。 #souper_fancy 是什么元素?

$("#souper_fancy").focus(function() {
    $(this).select();
});

这是一个 type="text" 元素。我也试过 $("input[type=text]") 。仍然无法在 Safari 中使用 jQuery 1.3.2。
B
Ben Wheeler

只是阻止 mouseup 的默认设置会导致文本选择始终处于打开状态。 MOUSEUP 事件负责清除文本选择。但是,通过阻止其默认行为,您无法使用鼠标取消选择文本。

为避免这种情况并让文本选择再次起作用,您可以在 FOCUS 上设置一个标志,从 MOUSEUP 读取它并重置它,以便将来的 MOUSEUP 事件按预期工作。

$("#souper_fancy").focus(function() {
    $(this).select();

    //set flag for preventing MOUSEUP event....
    $this.data("preventMouseUp", true);
});

$("#souper_fancy").mouseup(function(e) {
    var preventEvent = $this.data("preventMouseUp");

    //only prevent default if the flag is TRUE
    if (preventEvent) {
        e.preventDefault();
    }

    //reset flag so MOUSEUP event deselect the text
    $this.data("preventMouseUp", false);
});

a
andrewh

在对 requestAnimationFrame() 的回调中使用 setSelectionRange()

$(document).on('focus', '._selectTextOnFocus', (e) => {
    var input = e.currentTarget;
    var initialType = e.currentTarget.type;

    requestAnimationFrame(() => {
        // input.select() is not supported on iOS
        // If setSelectionRange is use on a number input in Chrome it throws an exception,
        // so here we switch to type text first.
        input.type = "text";
        input.setSelectionRange(0, Number.MAX_SAFE_INTEGER || 9999);
        input.type = initialType;
    });
});

使用 setSelectionRange() 而不是 select(),因为 select() 在移动 Safari 中不起作用(请参阅 Programmatically selecting text in an input field on iOS devices (mobile Safari))。

在选择文本之前必须使用 requestAnimationFrame 等待,否则在 iOS 上出现键盘后元素无法正确滚动到视图中。

使用 setSelectionRange() 时,将输入类型设置为 text 很重要,否则可能会在 Chrome 上引发异常(请参阅 selectionStart/selectionEnd on input type="number" no longer allowed in Chrome)。


C
Community

虽然这适用于在 IE、Firefox、Chrome、Safari 和 Opera 中选择它,但它不允许您通过在 Firefox、Chrome 和 Safari 中再次单击来编辑它。不完全确定,但我认为这可能是由于这 3 个浏览器重新发出焦点事件,即使该字段已经具有焦点,因此永远不允许您实际插入光标(因为您再次选择它),而在 IE和 Opera 它似乎没有这样做,因此焦点事件没有再次被触发,因此光标被插入。

我找到了一个更好的解决方案 in this Stack post,它没有该问题并且适用于所有浏览器。


r
rubiwachs

这也应该在 chrome 中工作:

$("#souper_fancy").focus(function() {
    var tempSouper = $(this);
    setTimeout(function(){
        tempSouper.select();
    },100);
});

请考虑添加建设性反馈,说明 OP 为什么会看到问题以及您的解决方案可以解决问题。
u
user2941872

因为使用 setTimeout 时会出现闪烁,所以还有另一种基于事件的解决方案。这样,'focus' 事件会附加 'mouseup' 事件,并且事件处理程序会再次分离自身。

    function selectAllOnFocus(e) {
    if (e.type == "mouseup") { // Prevent default and detach the handler
        console.debug("Mouse is up. Preventing default.");
        e.preventDefault();
        $(e.target).off('mouseup', selectAllOnFocus);
        return;
    }
    $(e.target).select();
    console.debug("Selecting all text");
    $(e.target).on('mouseup', selectAllOnFocus);
}

然后连接第一个事件

    $('.varquantity').on('focus', selectAllOnFocus);

C
Claudio

如果有人再次遇到这个问题,我在这里得到了一个纯 JS 解决方案,它(目前)适用于所有浏览器,包括。移动的

<input type="text" value="Hello world..." onFocus="window.setTimeout(() => this.select());">

(没有 setTimeout() 它不适用于 Safari、移动 Safari 和 MS Edge)