[英]Detect weather user clicked on text or white space inside textarea
我有一個可以拖動的文本區域,其中的文本仍然可以通過拖動來選擇。 為了區分“文本拖動”和“移動拖動”,我必須知道用戶開始拖動的位置(即鼠標按下位置)是空白還是文本。
我唯一能想到的就是使用字符寬度和行高進行計算,我想避免這種情況。
不太確定你在問什么,但這是我將精神錯亂轉化為工作代碼的嘗試:
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'm just going to put some text in here so we can run a very basic test. I hope this helps!</div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.