好吧,对于您的解决方案,您将需要使用selections
和doubleclick
事件作为一件棘手的事情,并通过双击事件在生成的范围内获取选定的单词。
如果您不想引入新标签,则没有其他方法。
尝试这个:
现场演示:http: //jsfiddle.net/oscarj24/5D4d3/
$(document).ready(function() {
/* set hand cursor to let know the user that this area is clickable */
var p = $('p');
p.css({ cursor: 'pointer' });
/* doubleclick event working with ranges */
p.dblclick(function(e) {
var selection = window.getSelection() || document.getSelection() || document.selection.createRange();
var word = $.trim(selection.toString());
if(word != '') {
alert(word);
}
/* use this if firefox: selection.collapse(selection.anchorNode, selection.anchorOffset); */
selection.collapse();
e.stopPropagation();
});
});
希望这可以帮助 :-)