Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.7k views
in Technique[技术] by (71.8m points)

如何阻止文本框再次聚焦当鼠标从划词弹出的图标栏移回文本框后

第一个脚本,当鼠标移到input、textarea元素的文本框,自动聚焦,光标在文字末尾,移出input、textarea会自动失焦

另一个脚本,划词弹图标栏

鼠标从图标栏移到文本框后,或者点击图标执行功能,比如复制,图标栏自动消失后,鼠标在文本框,会自动再次聚焦文本框,划词的文字会消失,光标在文字末尾

如何让鼠标从图标栏,移回到文本框不再自动聚焦,保留划词文字

有一个思路,使用油猴子存储本次鼠标所在位置的元素,移到下一个元素判断上一个元素是是img,就不自动聚焦,从input、textarea元素移到img元素清除失焦的计时

不知道如何存储本次的元素,作为下一次的判断

图标栏元素e.target.localName ==='img'

划词弹图标栏工具

//自动聚焦
document.addEventListener('mouseover', function (e) {
        if (e.target.localName ==='input' || e.target.localName ==='textarea'){
            e.target.focus();
            var val = e.target.value; //store the value of the element
            e.target.value = ''; //clear the value of the element
            e.target.value = val; //set that value back.
        }
    });
//失焦
    document.addEventListener('mouseout', function (e) {
        var text = window.getSelection().toString().trim();
        timer = setTimeout(function () {
            e.target.blur();
        }, 20)
        if(text){
            clearTimeout(timer);
        }
    },false);


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

判断输入框的selectionStart和end 一样就focus 不一样就说明有选择的然后就不focus 这样可以么


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...