欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript 選中文字并響應(yīng)獲取的實(shí)現(xiàn)代碼

 更新時(shí)間:2011年08月28日 17:53:28   作者:  
當(dāng)鼠標(biāo)選擇一段文字時(shí),對(duì)這個(gè)事件產(chǎn)生響應(yīng),并且將選中的文字傳遞出去。
本人不怎么會(huì)寫JS,但是會(huì)搜索,這里找到了些別人寫好的東西:
復(fù)制代碼 代碼如下:

select(document, tanchu);
/*=select[[
*
* 跨瀏覽器選中文字事件
* @param
* object o 響應(yīng)選中事件的DOM對(duì)象,required
* function fn(sText,target,mouseP)選中文字非空時(shí)的回調(diào)函數(shù),required
* |-@param
* |-sText 選中的文字內(nèi)容
* |-target 觸發(fā)mouseup事件的元素
* |-mouseP 觸發(fā)mouseup事件時(shí)鼠標(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);
}

 原作者見(jiàn):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ù)運(yùn)行后會(huì)將選取的文字返回出來(lái)?! ?

原作者已不可考。。。

相關(guān)文章

最新評(píng)論