检测页面上元素中选择(突出显示)或单击的内容? [英] Detect what is selected (highlighted) or clicked within an element on a page?

查看:101
本文介绍了检测页面上元素中选择(突出显示)或单击的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检测浏览器中页面上选择的内容?示例:单击,按住,在页面上选择3个单词和1个图像,然后发布。

How would one go about detecting what has been selected on a page in a browser? Example: Click, hold, select 3 words and 1 image on a page, release.

子问题:如何检测有人点击的字母?

Sub-question: How to detect what letter someone clicked on?

不使用:一个跨度注入器打破一切或一个WYSIWYG插件

Without using: A span injector breaking everything up OR a WYSIWYG plugin

我希望这不仅仅是一个类型您无法检测到的浏览器交互。

I'm hoping this isn't just a type of browser interaction you can't detect.

可能有很多用途,但我的目标是一个简单的实时页面编辑器,或者至少是一种了解什么的方法有人点击/选择除了元素的id。

There could be many uses, but my goal is a simple 'live' page editor, or at least a way to know what someone is clicking on/selecting aside from just the id of an element.

推荐答案

window.getSelection() 是您需要的除IE之外的所有主要浏览器< 9,它有不同的API。 window.getSelection()返回 选择 spec )您可以从中提取 DOM Range 规范)代表选择。

window.getSelection() is what you need in all major browsers except IE < 9, which has a different API. window.getSelection() returns a Selection (spec) object from which you can extract a DOM Range (spec) representing the selection.

对于IE< 9,你可以看一下 document.selection ,或使用我自己的库 Rangy ,在所有主流浏览器中提供相同的基于标准的API。

For IE < 9, you can look at document.selection, or use a library such as my own Rangy, which provides the same standards-based API in all major browsers.

这篇关于检测页面上元素中选择(突出显示)或单击的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆