我正在尝试在用户选择一些文本后显示工具提示。在相应的div中点击几下后,我发现工具提示出现的顺序被反转,因为它有时显示没有选择任何内容。关于如何解决这个问题的任何建议?
<script>
$('.test').click(function (e) {
console.log('click');
// RETURN HTML OF SELECTION
var html = "";
if (typeof window.getSelection != "undefined") {
var sel = window.getSelection();
if (sel.rangeCount) {
var container = document.createElement("div");
for (var i = 0, len = sel.rangeCount; i < len; ++i) {
container.appendChild(sel.getRangeAt(i).cloneContents());
}
html = container.innerHTML;
}
}
else if (typeof document.selection != "undefined") {
if (document.selection.type == "Text") {
html = document.selection.createRange().htmlText;
}
}
console.log(html);
if (html == "") {}
else {
console.log('right before popover');
$(".test").popover();
html = "";
}
});