本人不怎么会写JS,但是会搜索,这里找到了些别人写好的东西:
复制代码 代码如下:
select(document, tanchu);
/*=select[[
*
* 跨浏览器选中文字事件
* @param
* object o 响应选中事件的DOM对象,required
* function fn(sText,target,mouseP)选中文字非空时的回调函数,required
* |-@param
* |-sText 选中的文字内容
* |-target 触发mouseup事件的元素
* |-mouseP 触发mouseup事件时鼠标坐标
*/
function select(o, fn){
o.onmouseup = function(e){
var event = window.event || e;
var target = event.srcElement ? event.srcElement : event.target;
if (/input|textarea/i.test(target.tagName) && /firefox/i.test(navigator.userAgent)) {
//Firefox在文本框内选择文字
var staIndex=target.selectionStart;
var endIndex=target.selectionEnd;
if(staIndex!=endIndex){
var sText=target.value.substring(staIndex,endIndex);
fn(sText,target);
}
}
else{
//获取选中文字
var sText = document.selection == undefined ? document.getSelection().toString():document.selection.createRange().text;
if (sText != "") {
//将参数传入回调函数fn
fn(sText, target);
}
}
}
}
/*]]select=*/
function tanchu(txt,tar){
alert("文字属于"+tar.tagName+"元素,选中内容为:"+txt);
}

 原作者见:http://momomolice.com/wordpress/archives/420.html


附:只获得选取的文字的代码(不响应该事件)
复制代码 代码如下:
function getSelectedText()
{
if (window.getSelection)
{ // This technique is the most likely to be standardized.
// getSelection() returns a Selection object, which we do not document.
return window.getSelection().toString();
}
else if (document.getSelection)
{
// This is an older, simpler technique that returns a string
return document.getSelection();
}
else if (document.selection)
{
// This is the IE-specific technique.
// We do not document the IE selection property or TextRange objects.
return document.selection.createRange().text;
}
}

函数运行后会将选取的文字返回出来。  

原作者已不可考。。。
标签:
选中文字

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com

P70系列延期,华为新旗舰将在下月发布

3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。

而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?

根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。