如何使用JavaScript / jQuery选择(突出显示)表中的所有文本?
<table id="HighlightThis">
<thead>
<tr><th>Num.<th>Name
</thead>
<tbody>
<tr><td>1.<td>Cat
<tr><td>2.<td>Bird
<tr><td>3.<td>Fish
</tbody>
答案 0 :(得分:0)
尝试此功能应适用于所有主流浏览器,信用Tim Down
function selectElementContents(el) {
var body = document.body, range, sel;
if (document.createRange && window.getSelection) {
range = document.createRange();
sel = window.getSelection();
sel.removeAllRanges();
try {
range.selectNodeContents(el);
sel.addRange(range);
} catch (e) {
range.selectNode(el);
sel.addRange(range);
}
} else if (body.createTextRange) {
range = body.createTextRange();
range.moveToElementText(el);
range.select();
}
}
请参阅fiddle