首页 > 解决方案 > 检测天气用户点击文本区域内的文本或空白

问题描述

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

我唯一能想到的就是使用字符宽度和行高来计算,我想避免这种情况。

标签: javascripthtmltextarea

解决方案


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

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>


推荐阅读