gpt4 book ai didi

javascript - 如何将弹出窗口定位在突出显示的文本部分上?

转载 作者:行者123 更新时间:2023-11-30 07:04:36 25 4
gpt4 key购买 nike

假设有一段文本的场景。而且,在突出显示其中的一个单词时,我希望弹出窗口显示工具提示,该提示位于突出显示的单词处。

有点像 mac 如何显示如下单词的定义 - highlight example

这是一个 angularjs 应用程序,我正在使用 angularui。因此,以 angular ui 为中心的解决方案是可取的,但不是必需的。

提前致谢。

最佳答案

你可以使用下面的代码来做你想做的事......祝你好运:

<html>

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

<script>
function getSelectedText() {
var text = "";
if (typeof window.getSelection != "undefined") {
text = window.getSelection().toString();
} else if (typeof document.selection != "undefined" && document.selection.type == "Text") {
text = document.selection.createRange().text;
}
return text;
}

function doSomethingWithSelectedText() {
var selectedText = getSelectedText();
if (selectedText) {

$('#infoDiv').css('display', 'block');
$('#infoDiv').css('position', 'absolute');
$('#infoDiv').css('left', event.clientX + 10);
$('#infoDiv').css('top', event.clientY + 15);
} else {
$('#infoDiv').css('display', 'none');
};
};

document.onmouseup = doSomethingWithSelectedText;
document.onkeyup = doSomethingWithSelectedText;
</script>
<style>
/*Tooltip div styling */
.tooltipDiv {
display: none;
width: 250px;
z-index: 101;
background-color: #fff;
border: 3px solid #666;
padding: 12px 12px 12px 12px;
border-radius: 0px 25px 0px 25px;
}
</style>

</head>

<body>
<div id="infoDiv" class="tooltipDiv">This is where your will put whatever you like...</div>

<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
</body>

</html>

关于javascript - 如何将弹出窗口定位在突出显示的文本部分上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28246522/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com