<button id="btn">获取选中值</button>
<div id="show"></div>
<script>
String.prototype.trim = function() {
return this.replace(/^/s+|/s+$/g, "");
}
/* 方法一 FF下有点问题 */
function getSelectText() {
try{
// IE: document.selection.createRange() W3C:window.getSelection()
var selectText = (document.selection && document.selection.createRange )? document.selection.createRange().text : window.getSelection().toString();
if(selectText != null && selectText.trim() != ""){
return selectText;
}
}catch(err){}
}
/* 方法二 */
function getSelectText2(id) {
var t = document.getElementById(id);
if(window.getSelection) {
if(t.selectionStart != undefined && t.selectionEnd != undefined) {
return t.value.substring(t.selectionStart, t.selectionEnd);
} else {
return "";
}
} else {
return document.selection.createRange().text;
}
}
document.getElementById('btn').onclick = function() {
document.getElementById('show').innerHTML = getSelectText2('content');
}
</script>
</body>
</html>
新闻热点
疑难解答