检测天气用户单击文本区域内的文本或空白

问题描述

我有一个文本区域,可以在其中拖动,仍然可以通过在其上拖动来选择文本。为了区分“超文本拖动”与“移动拖动”,我必须知道天气用户开始拖动的点(即,鼠标向下的位置)是空白还是文本。

我想出的唯一办法就是使用字符宽度和行高来计算,这是我想避免的。

解决方法

不太确定您要问的是什么,但这是我将精神错乱转换为工作代码的尝试:

let doc,htm,bod,I; // for use on other loads
addEventListener('load',()=>{
doc = document; htm = doc.documentElement; bod = doc.body; I = id=>doc.getElementById(id);
// magic under here
const test = I('test');
function whiteStart(string){
  if(string.match(/^\s+/)){
    return true;
  }
  return false;
}
test.oncopy = test.ondragstart = function(){
  let s = getSelection().toString();
  if(whiteStart(s)){
    console.log('"'+s+'" starts with one or more white spaces');
  }
  else{
    console.log('"'+s+'" starts without a white space');
  }
}
}); // end load
<div id='test'>I&apos;m just going to put some text in here so we can run a very basic test. I hope this helps!</div>