- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="content-type" content="text/html; charset=utf-8" />
- <meta name="keywords" content="selection, selectionStart, getSelection, select, input, " />
- <meta name="description" content="在一些特殊應(yīng)用中,我們需要獲取頁面上選中的文字,但是要實現(xiàn)這一需求,我們不得不面對那惱人的兼容
問題,本文介紹了一個兼容性較好的解決方法。同時,也提供了一個在 FireFox 下獲取 input 和 textarea 中選中文字的解決方
案。" />
- <title>Javascript 獲取頁面上選中的文
字 - selection, selectionStart, getSelection, select, input, </title>
- </head>
- <body>
- <div id="example">
- <h3 id="example_title">Javascript 獲取頁面上選中的文字</h3>
- <div id="example_main">
-
- <script type="text/javascript">
- 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;
- }
- }
- function getTextFieldSelection(e) {
- if (e.selectionStart != undefined && e.selectionEnd != undefined) {
- var start = e.selectionStart;
- var eend = e.selectionEnd;
- return e.value.substring(start, end);
- }
- else return ""; // Not supported on this browser
- }
- function doGetSelectedText() {
- var text = getSelectedText();
- document.getElementById('output').innerHTML = text;
- }
- function doGetTextFieldSelection() {
- var el = document.getElementById('tempText');
- var text = getTextFieldSelection(el);
- document.getElementById('output').innerHTML = text;
- }
- window.onload = function() {
- document.getElementById('getSelectedText').onclick = doGetSelectedText;
- document.getElementById('getTextFieldSelection').onclick = doGetTextFieldSelection;
- }
- </script>
- <h2>請
選中頁面上的文字后點擊下面的按鈕:</h2>
- <div id="output" style="color:#FF0000;font-weight:bold;"></div>
- <p>在
編程的過程中,我們通常都會積累很多簡單、有效并且可重用的小段代碼,一個簡單的字符串處理函數(shù)或者一個驗證郵件地址的正則表達式,又或者一個簡單的文件
上傳類,甚至一個效果不錯的CSS導航樣式。這些小技巧為我們節(jié)省了不少時間,但是時間一長,代碼數(shù)量越來越多,尋找起來也耗費了不少時間。因此,本站致
力于收集整理一些類似的小知識,并且努力提高文章搜索質(zhì)量,一來方便大家查閱,二來也算是支持一下開源事業(yè)。</p>
- <p>本
站收集的代碼和教程中,有從世界著名開源軟件中摘取的函數(shù)、類,也有網(wǎng)友提交的原創(chuàng)或翻譯的精彩文章。本站的收錄代碼的標準是:簡單、精彩、通用。</p>
- <br />
- <textarea rows="8" cols="80" id="tempText">在編程的過程中,我們通常都會積累很多簡單、有效并且可重用的小段代碼,一個簡單的字符串
處理函數(shù)或者一個驗證郵件地址的正則表達式,又或者一個簡單的文件上傳類,甚至一個效果不錯的CSS導航樣式。</textarea>
- <br /><br />
- <input type="button" value="獲取選中的內(nèi)容" id="getSelectedText" />
- <input type="button" value="FireFox 下獲取文本框里選中的內(nèi)容" id="getTextFieldSelection" />
- <br />
-
- </div>
- </div>
- <br />
- </body>
- </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="keywords" content="selection, selectionStart, getSelection, select, input, " />
<meta name="description" content="在一些特殊應(yīng)用中,我們需要獲取頁面上選中的文字,但是要實現(xiàn)這一需求,我們不得不面對那惱人的兼容問題,本文介紹了一個兼容性較好的解決方法。同時,也提供了一個在 FireFox 下獲取 input 和 textarea 中選中文字的解決方案。" />
<title>Javascript 獲取頁面上選中的文字 - selection, selectionStart, getSelection, select, input, </title>
</head>
<body>
<div id="example">
<h3 id="example_title">Javascript 獲取頁面上選中的文字</h3>
<div id="example_main">
<!--************************************* 實例代碼開始 *************************************-->
<script type="text/javascript">
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;
}
}
function getTextFieldSelection(e) {
if (e.selectionStart != undefined && e.selectionEnd != undefined) {
var start = e.selectionStart;
var end = e.selectionEnd;
return e.value.substring(start, end);
}
else return ""; // Not supported on this browser
}
function doGetSelectedText() {
var text = getSelectedText();
document.getElementById('output').innerHTML = text;
}
function doGetTextFieldSelection() {
var el = document.getElementById('tempText');
var text = getTextFieldSelection(el);
document.getElementById('output').innerHTML = text;
}
window.onload = function() {
document.getElementById('getSelectedText').onclick = doGetSelectedText;
document.getElementById('getTextFieldSelection').onclick = doGetTextFieldSelection;
}
</script>
<h2>請選中頁面上的文字后點擊下面的按鈕:</h2>
<div id="output" style="color:#FF0000;font-weight:bold;"></div>
<p>在編程的過程中,我們通常都會積累很多簡單、有效并且可重用的小段代碼,一個簡單的字符串處理函數(shù)或者一個驗證郵件地址的正則表達式,又或者一個簡單的文件上傳類,甚至一個效果不錯的CSS導航樣式。這些小技巧為我們節(jié)省了不少時間,但是時間一長,代碼數(shù)量越來越多,尋找起來也耗費了不少時間。因此,本站致力于收集整理一些類似的小知識,并且努力提高文章搜索質(zhì)量,一來方便大家查閱,二來也算是支持一下開源事業(yè)。</p>
<p>本站收集的代碼和教程中,有從世界著名開源軟件中摘取的函數(shù)、類,也有網(wǎng)友提交的原創(chuàng)或翻譯的精彩文章。本站的收錄代碼的標準是:簡單、精彩、通用。</p>
<br />
<textarea rows="8" cols="80" id="tempText">在編程的過程中,我們通常都會積累很多簡單、有效并且可重用的小段代碼,一個簡單的字符串處理函數(shù)或者一個驗證郵件地址的正則表達式,又或者一個簡單的文件上傳類,甚至一個效果不錯的CSS導航樣式。</textarea>
<br /><br />
<input type="button" value="獲取選中的內(nèi)容" id="getSelectedText" />
<input type="button" value="FireFox 下獲取文本框里選中的內(nèi)容" id="getTextFieldSelection" />
<br />
<!--************************************* 實例代碼結(jié)束 *************************************-->
</div>
</div>
<br />
</body>
</html>