简体   繁体   English

在Safari Mobile上以编程方式选择DOM元素(就像使用鼠标一样)?

[英]Programmatically select DOM element (as if with mouse) on Safari Mobile?

Would like to programmatically select HTML within a DOM element, as if the user had selected with a mouse, precisely to avoid making them select with a mouse. 想要以编程方式在DOM元素中选择HTML,就像用户使用鼠标选择一样,精确地避免使用鼠标选择它们。

This bit of elegant code from SO post ( Select all DIV text with single mouse click ) works great on laptop browsers I tested (IE, Chrome, FF, Safari on Windows and Mac): 来自SO post的这一优雅代码( 单击鼠标选择所有DIV文本 )在我测试的笔记本电脑浏览器(IE,Chrome,FF,Windows和Mac上的Safari)上运行良好:

    function selectText(el) {
        if (document.selection) {
            var range = document.body.createTextRange();
                range.moveToElementText(el);
            range.select();
            console.log("select 1");
        } else if (window.getSelection) {
            var range = document.createRange();
            range.selectNode(el);
            window.getSelection().addRange(range);
                        console.log("select 2");
        }
        else {
          console.log("select 3");
        }
    };

JSFiddle: http://jsfiddle.net/z4yMh/7/ JSFiddle: http //jsfiddle.net/z4yMh/7/

But does not work on Safari mobile (see JSFiddle). 但是在Safari移动设备上不起作用(参见JSFiddle)。 The mobile dev console shows he console shows select 2 indicating the click event is getting called, mobile dev console shows no error (ie the methods selectNode() don't seem to be null), just nothing happening. 移动开发者控制台显示他的控制台显示select 2表示点击事件正在调用,移动开发控制台显示没有错误(即方法selectNode()似乎不为空),只是没有发生任何事情。

Can't guess why. 猜不出为什么。 Googling is hard as select is also used to refer to a different concept jQuery/DOM selectors. 谷歌搜索很难,因为select也用于指代不同的概念jQuery / DOM选择器。

What I'm hoping for is an effect that's like native selection in Safari mobile, as in the picture here: 我希望的是一种效果,就像Safari移动中的原生选择一样,如下图所示:

在此输入图像描述

This project does not use jQuery, but if that solves the problem jQuery would be fine. 这个项目不使用jQuery,但如果这解决了问题,jQuery就可以了。

According to the CSS Ninja 根据CSS Ninja

When setting a range iOS Safari won't actually show the selection as highlighted but if you were to check the document selection it would return the correct content, desktop browsers will show the range selected in the document. 设置范围时,iOS Safari实际上不会将选择显示为突出显示,但如果您要检查文档选择它将返回正确的内容,桌面浏览器将显示文档中选择的范围。

However if you do the same with a user action like tapping the “set selection range” button in the demo the iOS highlight will show up. 但是,如果您通过点击演示中的“设置选择范围”按钮等用户操作执行相同操作,则会突出显示iOS高亮显示。 Another interesting quirk is if I tap the content and bring the keyboard up but don't dismiss it then refresh the page the programmatically set selection will show the iOS selection highlight. 另一个有趣的怪癖是,如果我点击内容并启动键盘,但不要忽略它,然后刷新页面,编程设置选择将显示iOS选择突出显示。

Another interesting find is if you perform execCommand, which I'll touch on later in the article, like bold it will apply the command to the selection made and make the iOS selection UI appear. 另一个有趣的发现是,如果你执行execCommand,我将在本文后面介绍,如粗体,它将命令应用于所做的选择,并使iOS选择UI出现。

Hope this helps. 希望这可以帮助。

I agree with @nietonfir but I also updated the jsfiddle, a few times, to see how it would react. 我同意@nietonfir,但我也更新了jsfiddle,几次,看看它会如何反应。

The important point is to replace "click" with one of: touchstart, touchmove, touchend, touchcancel. 重要的是用以下之一替换“click”:touchstart,touchmove,touchend,touchcancel。

element.addEventListener('touchstart', function(e) {...});

See it in action here (minus the Mobile Safari selection UI): 在此处查看它(减去Mobile Safari选择UI):

http://jsfiddle.net/z4yMh/16/ http://jsfiddle.net/z4yMh/16/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM