JavaScript 選中文字并響應獲取的實現(xiàn)代碼
更新時間:2011年08月28日 17:53:28 作者:
當鼠標選擇一段文字時,對這個事件產生響應,并且將選中的文字傳遞出去。
本人不怎么會寫JS,但是會搜索,這里找到了些別人寫好的東西:
select(document, tanchu);
/*=select[[
*
* 跨瀏覽器選中文字事件
* @param
* object o 響應選中事件的DOM對象,required
* function fn(sText,target,mouseP)選中文字非空時的回調函數(shù),required
* |-@param
* |-sText 選中的文字內容
* |-target 觸發(fā)mouseup事件的元素
* |-mouseP 觸發(fā)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 != "") {
//將參數(shù)傳入回調函數(shù)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;
}
}
函數(shù)運行后會將選取的文字返回出來。
原作者已不可考。。。
復制代碼 代碼如下:
select(document, tanchu);
/*=select[[
*
* 跨瀏覽器選中文字事件
* @param
* object o 響應選中事件的DOM對象,required
* function fn(sText,target,mouseP)選中文字非空時的回調函數(shù),required
* |-@param
* |-sText 選中的文字內容
* |-target 觸發(fā)mouseup事件的元素
* |-mouseP 觸發(fā)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 != "") {
//將參數(shù)傳入回調函數(shù)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;
}
}
函數(shù)運行后會將選取的文字返回出來。
原作者已不可考。。。
相關文章
JavaScript利用Immerjs實現(xiàn)不可變數(shù)據(jù)
Immerjs?是一個用于管理?JavaScript?不可變數(shù)據(jù)結構的庫,它可以幫助我們更輕松地處理狀態(tài)的變化,并減少冗余代碼。本文就來帶大家揭秘如何利用Immerjs實現(xiàn)不可變數(shù)據(jù),感興趣的可以了解一下2023-04-04使用Bootstrap typeahead插件實現(xiàn)搜索框自動補全的方法
這篇文章主要介紹了使用Bootstrap typeahead插件實現(xiàn)搜索框自動補全的方法的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-07-07JavaScript中實現(xiàn)數(shù)組分組功能的方法詳解
最近,JavaScript引入了一個備受期待的功能:原生支持數(shù)組分組,這一特性使得在處理復雜的數(shù)據(jù)集時變得更加簡單和高效,本文將深入探討這一全新的JavaScript特性,希望對大家有所幫助2023-12-12