您可以做類似這樣的事情,與您所做的事情不太一樣之後,我想?但可能會讓你開始進一步的想法。
<div>In cryptography, a keyed-hash message authentication code (HMAC) is a specific construction for calculating a message authentication code (MAC) involving a cryptographic hash function in combination with a secret cryptographic key. As with any MAC, it may be used to simultaneously verify both the data integrity and the authentication of a message. Any cryptographic hash function, such as MD5 or SHA-1, may be used in the calculation of an HMAC; the resulting MAC algorithm is termed HMAC-MD5 or HMAC-SHA1 accordingly. The cryptographic strength of the HMAC depends upon the cryptographic strength of the underlying hash function, the size of its hash output, and on the size and quality of the key.</div>
<button id="get">Get Selected</button>
function getText() {
var selectedText
if (typeof window.getSelection === "function") {
selectedText = window.getSelection();
} else if (typeof document.getSelection === "function") {
selectedText = document.getSelection();
} else if (document.selection && typeof document.selection.createRange() === "function") {
selectedText = document.selection.createRange().text;
} else {
selectedText = "";
alert("No method to get selected text");
}
if (!selectedText || selectedText === "") {
if (document.activeElement.selectionStart) {
selectedText = document.activeElement.value.substring(
document.activeElement.selectionStart.document.activeElement.selectionEnd);
}
}
alert(selectedText);
}
document.getElementById("get").addEventListener("click", getText, false);
上jsfiddle
你也可以看到進一步的答案在那裏我有這個想法
here on SO擴大。
筆者拉到其他的問題,但here is the other jsfiddle
window.getSelection
摘要
返回表示文本的由 用戶選擇的範圍內的選擇對象。
規格
DOM級別0.不是任何標準的組成部分。
預計在新的DOM範圍規範中指定
還有一個叫Rangy庫應該處理這種薄跨瀏覽器的,從來沒有嘗試過,但你可能要看一看。
一個跨瀏覽器的JavaScript範圍和選擇庫。它提供了一個簡單的基於標準的API,用於在所有主流瀏覽器中執行常見的DOM範圍和 選擇任務,從而在互聯網 Explorer最高版本(包括版本8)和DOM兼容瀏覽器之間抽象化地實現了該功能。
http://stackoverflow.com/about – Xotic750 2013-05-11 09:57:38