如何从另一个span / div / table对象的'a'标签中选择所需范围内的所有数据?
<td>
<span class='servkey'>texttoselect</span>
<a href='#' title='Select all'>select all</a>
</td>
谢谢!
答案 0 :(得分:1)
以下代码只选择span
var span = $('#your_span_id')[0],
sel, range;
if(window.getSelection){
sel = window.getSelection();
range = document.createRange();
range.selectNode(span);
sel.removeAllRanges();
sel.addRange(range);
} else {
range = document.body.createTextRange();
range.moveToElementText(span);
range.select()
}
小提琴:here