JS案例:页面显示鼠标指向的单词
来自CloudWiki
<!doctype html> <html> <head> <meta charset="utf-8"> <title>获取鼠标指向的单词</title> <script type="text/javascript"> function catchWord() {//抓词 var word = document.body.createTextRange(); //创建一个基于 <body> 元素的文本范围 word word.moveToPoint(event.clientX, event.clientY);//把文本范围 word 移至鼠标位置 word.expand("word");//扩展文本范围 word,使之包含一个完整的单词 window.status = word.text;//取出文本范围 word 中的文本 } </script> </head> <body onmousemove="catchWord()"> <p>通过调用TextRange对象的miveStart、moveEnd、move、moveToPoint和expand等方法可以改变这两个指针的位置,从而改变文本范围的位置和大小。</p> </body> </html>