JavaScript 選中文字并響應(yīng)獲取的實現(xiàn)代碼
更新時間:2011年08月28日 17:53:28 作者:
當(dāng)鼠標(biāo)選擇一段文字時,對這個事件產(chǎn)生響應(yīng),并且將選中的文字傳遞出去。
本人不怎么會寫JS,但是會搜索,這里找到了些別人寫好的東西:
select(document, tanchu);
/*=select[[
*
* 跨瀏覽器選中文字事件
* @param
* object o 響應(yīng)選中事件的DOM對象,required
* function fn(sText,target,mouseP)選中文字非空時的回調(diào)函數(shù),required
* |-@param
* |-sText 選中的文字內(nèi)容
* |-target 觸發(fā)mouseup事件的元素
* |-mouseP 觸發(fā)mouseup事件時鼠標(biāo)坐標(biāo)
*/
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在文本框內(nèi)選擇文字
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 != "") {
//將參數(shù)傳入回調(diào)函數(shù)fn
fn(sText, target);
}
}
}
}
/*]]select=*/
function tanchu(txt,tar){
alert("文字屬于"+tar.tagName+"元素,選中內(nèi)容為:"+txt);
}
原作者見:http://momomolice.com/wordpress/archives/420.html
附:只獲得選取的文字的代碼(不響應(yīng)該事件)
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;
}
}
函數(shù)運行后會將選取的文字返回出來?! ?
原作者已不可考。。。
復(fù)制代碼 代碼如下:
select(document, tanchu);
/*=select[[
*
* 跨瀏覽器選中文字事件
* @param
* object o 響應(yīng)選中事件的DOM對象,required
* function fn(sText,target,mouseP)選中文字非空時的回調(diào)函數(shù),required
* |-@param
* |-sText 選中的文字內(nèi)容
* |-target 觸發(fā)mouseup事件的元素
* |-mouseP 觸發(fā)mouseup事件時鼠標(biāo)坐標(biāo)
*/
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在文本框內(nèi)選擇文字
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 != "") {
//將參數(shù)傳入回調(diào)函數(shù)fn
fn(sText, target);
}
}
}
}
/*]]select=*/
function tanchu(txt,tar){
alert("文字屬于"+tar.tagName+"元素,選中內(nèi)容為:"+txt);
}
原作者見:http://momomolice.com/wordpress/archives/420.html
附:只獲得選取的文字的代碼(不響應(yīng)該事件)
復(fù)制代碼 代碼如下:
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;
}
}
函數(shù)運行后會將選取的文字返回出來?! ?
原作者已不可考。。。
您可能感興趣的文章:
相關(guān)文章
JavaScript判斷數(shù)組重復(fù)內(nèi)容的兩種方法(推薦)
本文給大家介紹兩種JavaScript判斷數(shù)組重復(fù)內(nèi)容的方法(推薦)非常不錯具有參考借鑒價值,感興趣的朋友一起看看吧2016-06-06
Js 回車換行處理的辦法及replace方法應(yīng)用
當(dāng)我們在文本框里輸入文字敲下回車后,希望在提交后網(wǎng)頁也顯示是換行的效果,可往往并不能如愿以償啊,實在是憤怒啊自己寫了一個回車換行處理的函數(shù),感興趣的朋友可以了解下啊,希望本文對你有所幫助2013-01-01
基于Node.js的JavaScript項目構(gòu)建工具gulp的使用教程
也許你使用過grunt,那么這里來安利gulp的話就更加不會陌生了,下面我們就來看一下基于Node.js的JavaScript項目構(gòu)建工具gulp的使用教程2016-05-05

