javascript - 检测天气用户点击文本区域内的文本或空白
问题描述
我有一个可以拖动的文本区域,其中的文本仍然可以通过拖动来选择。为了区分“文本拖动”和“移动拖动”,我必须知道用户开始拖动的点(即鼠标按下位置)是空白或文本。
我唯一能想到的就是使用字符宽度和行高来计算,我想避免这种情况。
解决方案
不太确定你在问什么,但这是我将精神错乱转化为工作代码的尝试:
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>
推荐阅读
- javascript - 如何使用 QuickChart 更改 Chart.js 图像渲染的字体和轴标签?
- angular - Ionic/Angular - 大屏幕的菜单图标消失
- python - 为什么包含 futures.ThreadpoolExecutor 的类方法在某些情况下不运行?
- kubernetes - kubectl exec 进入容器而不使用 pod 名称末尾的随机 guid
- python - 检查csv文件是否存在并满足条件?
- three.js - Raycaster 无法检测到立方体
- mysql - WooCommerce:使用 SQL 查询删除订单范围
- javascript - 您可以在 React JS 应用程序的新选项卡中显示 PDF 文件吗
- css - CSS背景图像未显示,尽管正确的路径和命名是正确的
- php - 从实时统计数据创建视图