div中的选定文本 [英] Selected text inside div
本文介绍了div中的选定文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望页面加载时突出显示<div>
中的文本(选择要复制).
I want this text inside <div>
to be highlighten (selected for copy) when page loads.
<div id='sample_div'>This is sample text</div>
我尝试过的事情:
$(document).ready(function(){
$('#sample_div').select();
});
推荐答案
因此,如果我正确假设,则希望突出显示特定div中的文本.
So if I assume correctly, you want the text inside a particular div to be highlighted..
function selectText(element) {
var doc = document
, text = doc.getElementById(element)
, range, selection
;
if (doc.body.createTextRange) { //ms
range = doc.body.createTextRange();
range.moveToElementText(text);
range.select();
} else if (window.getSelection) { //all others
selection = window.getSelection();
range = doc.createRange();
range.selectNodeContents(text);
selection.removeAllRanges();
selection.addRange(range);
}
}
工作 jsFiddle
Source
这篇关于div中的选定文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文