假设有一个mousestop
事件附加到整个文档,找出确切事件的最佳方法是什么word当鼠标停止移动时,在光标下方(如果有任何文本)?
我可以从事件处理程序获取底层(jQuery)元素 -$(document.elementFromPoint(e.clientX, e.clientY))
- 但接下来会发生什么呢?
到目前为止,我的想法是用其副本替换 hit 元素中的所有文本节点,其中每个单词都包含在 DOM 元素中(还不知道是哪一个),然后调用$(document.elementFromPoint(e.clientX, e.clientY))
再次获取仅包含鼠标下的单词的元素。
但这似乎是一个复杂的计划,我想知道我是否错过了一些更简单的东西。
好吧,到目前为止还没有什么神奇的技巧,所以这是一个沉闷无聊(但有效)的解决方案:
$(document.body).mousemove(function(e){
var onmousestop = function() {
function getHitWord(hit_elem) {
var hit_word = '';
hit_elem = $(hit_elem);
//text contents of hit element
var text_nodes = hit_elem.contents().filter(function(){
return this.nodeType == Node.TEXT_NODE && this.nodeValue.match(/[a-zA-Z]{2,}/)
});
//bunch of text under cursor? break it into words
if (text_nodes.length > 0) {
var original_content = hit_elem.clone();
//wrap every word in every node in a dom element
text_nodes.replaceWith(function(i) {
return $(this).text().replace(/([a-zA-Z-]*)/g, "<word>$1</word>")
});
//get the exact word under cursor
var hit_word_elem = document.elementFromPoint(e.clientX, e.clientY);
if (hit_word_elem.nodeName != 'WORD') {
console.log("missed!");
}
else {
hit_word = $(hit_word_elem).text();
console.log("got it: "+hit_word);
}
hit_elem.replaceWith(original_content);
}
return hit_word;
}
var hit_word = getHitWord(document.elementFromPoint(e.clientX, e.clientY));
...
}
}
还涉及一些其他微妙之处(例如事件“降噪”、保持替换的 dom 上下文(例如选择)等等),但您明白了。
Here https://stackoverflow.com/questions/219322/how-can-i-use-javascript-timing-to-control-on-mouse-stop-and-on-mouse-move-events您可以了解如何设置 mousestop 事件。
EDIT:
在 ICE 中制作自定义 html 元素可能不是那么简单(谁会想到?)。查看更多here http://msdn.microsoft.com/en-us/library/ms531076(VS.85).aspx.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)