js获取鼠标选中的文字
1、获取选中的文字:
document.selection.createRange().text; IE9以下使用
window.getSelection().toString(); 其他浏览器使用
$('p').mouseup(function(){
var txt = window.getSelection?window.getSelection():document.selection.createRange().text;
alert(txt) ;
})
2、取消处于选中状态的文字:
document.selection.empty(); IE9以下使用
window.getSelection().removeAllRanges(); 其他浏览器使用
$('p').mouseup(function(){
window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
})
上述方法不仅对div或p标签中的文本有效(会自动忽略选中的‘图片’),在ie和chrome中对input中的文本也有效,但在firefox中无效,jquery
的.select()事件(仅对input有效)或js的onselect事件(仅对input有效)和js的.select()(使input中的文本内容处于选中状态)方法在三个浏览器中都有效。
可以用鼠标选中下段文字测试效果:
是他,是他,是他,就是他.我们的朋友小哪吒.是他,就是他,是他,就是他.少年英雄,小哪吒.上天他比~(稍长音),天要高~(同上).下海他比~(同上),海更大啊~啊~(同上).智斗妖魔~,勇降鬼怪.少年英雄,就是小哪吒.有时,他很聪明.有时,他也犯傻.
他的个头跟我一般高.有时,他很努力.有时,他也贪玩.他的年级和我一般大~~(长音).上天他比~(稍长音),天要高~(同上).下海他比~(同上),海更大啊~啊~(同上).智斗妖魔~,勇降鬼怪.少年英雄,就是小哪吒~~(长音).
3、使某Dom中的文字处于选中状态:
IE中关于range参见http://msdn.microsoft.com/en-us/library/ie/ms536401%28v=vs.85%29.aspx
$('.somedom').click(function(){
/* not ok for firefox
var selection = window.getSelection();
var range = document.createRange();
range.selectNodeContents(this);
selection.removeAllRanges();;
selection.addRange(range);*/
this.focus();
if(window.getSelection){
var range=document.createRange();
range.selectNodeContents(this);
var selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range)
}
else if(document.selection){
//for ie
var range=document.body.createTextRange()
range.moveToElementText(this)
range.select();
}
js获取鼠标选中的文字的更多相关文章
- js 获取鼠标选中值
if (window.getSelection) {//一般浏览器 userSelection = window.getSelection();} else if (document.selectio ...
- 原生js获取鼠标坐标方法全面讲解-zmq
原生js获取鼠标坐标方法全面讲解:clientX/Y,pageX/Y,offsetX/Y,layerX/Y,screenX/Y 一.关于js鼠标事件综合各大浏览器能获取到坐标的属性总共以下五种:eve ...
- js获取鼠标位置的各种方法
在一些DOM操作中我们经常会跟元素的位置打交道,鼠标交互式一个经常用到的方面,令人失望的是不同的浏览器下会有不同的结果甚至是有的浏览器下没结果,这篇文章就上鼠标点击位置坐标获取做一些简单的总结,没特殊 ...
- JS获取select选中的值,所有option值
<select name="myselect" id="myselect"> <option value="2042"&g ...
- js跟着鼠标移动的文字
废话不多说,直接上代码,有注释: <head> <title></title> <style type="text/css"> sp ...
- js获取select选中的内容
### 获取select选中的内容 js获取select标签选中的值 var obj = document.getElementById("selectId");//获取selec ...
- js获取鼠标坐标位置兼容多个浏览器
这个是IE 11 下兼容下视图测试时可用. $(window).bind('beforeunload', function (event) { var _this = this; var x = ev ...
- JS获取鼠标位置,兼容IE FF
由于Firefox和IE等浏览器之间对js解释的方式不一样,firefox下面获取鼠标位置不能够直接使用clientX来获取.网上说的一般都是触发mousemove事件才行.我这里有两段代码,思路都一 ...
- tinyMCE获取鼠标选中的值
今天遇到一个需求就是要在WordPress的文章编辑页面增加选中文字时要将选中值传递到弹出窗口中 解决方法: 去网上找到了下面这段代码,可以获取到选中文本 { type: 'textbox', nam ...
随机推荐
- javaWeb图片验证码代码
1. [代码]初始粗糙代码 import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.a ...
- AVAudioPlayer音频播放器-备用
IOS中有三种播放音频的方式:AVAudioPlayer.音频服务.音频队列. 此文主要讲AVAudioPlayer,其他两个请见相关文章. AVAudioPlayer在AVFoundation框架下 ...
- android更新SDK时候丢失annotations.jar 导致支持库报错
I am trying to update my Android SDK Tools to 17 rev. and I updated usign SDK Tools but in Propertie ...
- html标签引入外部html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- atof
So given a string like "2.23" your function should return double 2.23. This might seem eas ...
- 分布式文件系统 FastDFS Ceph
分布式文件系统 FastDFS Cephhttp://www.oschina.net/p/fastdfshttp://www.oschina.net/p/ceph FastDFS 的 Go 客户端 f ...
- 【转】Thunderbird中配置签名
原文网址:https://support.mozilla.org/zh-CN/kb/Thunderbird%E4%B8%AD%E9%85%8D%E7%BD%AE%E7%AD%BE%E5%90%8D “ ...
- Android 侧滑(双向滑动菜单)效果
下面看看我们如何使用它,达到我们想要的效果 public class MainActivity extends Activity { /** * 双向滑动菜单布局 */ private SliderM ...
- POJ1017 packets
Packets Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 48911 Accepted: 16570 Descrip ...
- Oracle 表分析
ANALYZE TABLE SeikyuTbl COMPUTE Statistics FOR TABLE FOR ALL COLUMNS FOR ALL INDEXES ; 一.优化器的优化方式 Or ...