在考虑浏览器兼容性时,在span标签内选择文本的方法是什么?例如,我有:在span标签内选择文本
jQuery().html('<p>Hello world <span>lorem ipsum</span> my good friend!');
我希望lorem ipsum
部分将被选择光标。
我有这个code它选择文本:
function SelectText(element) {
var doc = document
, text = doc.getElementById(element)
, range, selection
;
if (doc.body.createTextRange) {
range = document.body.createTextRange();
range.moveToElementText(text);
range.select();
} else if (window.getSelection) {
selection = window.getSelection();
range = document.createRange();
range.selectNodeContents(text);
selection.removeAllRanges();
selection.addRange(range);
}
}
不错的问题。我希望看到一种适用于移动浏览器的方法。 –
[选择元素中的文本(类似于使用鼠标突出显示)]的可能重复](http://stackoverflow.com/questions/985272/selecting-text-in-an-element-akin-to-highlighting-with-你的鼠标) – Harangue
你在说css中的'cursor:pointer'吗? –