javascript - 在突出显示时显示 DIV,然后隐藏

标签 javascript

我试图在突出显示特定文本时显示隐藏的 div 标签。我能够在突出显示时显示一个隐藏的 div,但我无法完成的两部分是:

  1. 仅在突出显示特定文本时显示(我假设使用跨度标记 ID 或类似的内容)

  2. 将显示更改为阻止后,5 秒后将其更改回隐藏。

这是我的尝试。同样,这确实显示了突出显示的隐藏 div,但据我所知。请帮忙!

 function ShowNote() {
   document.getElementById('Note').style.display = 'block';
 }

 document.onmouseup = ShowNote;
 if (!document.all) document.captureEvents(Event.MOUSEUP);

 function HideNote() {
   document.getElementById('Note').style.display = 'hidden';
 }
 setTimeout("HideNote()", 5000); // after 5 secs
I DON'T want it to show when I highlight this text
<br />I DO want it to show when I highlight this text.
<div type='text' id='Note' style="display:none;">HIDDEN DIV CONTENT</div>

最佳答案

你已经很接近了!

这是我所拥有的:

function ShowNote() {
    if(window.getSelection() == "I DO want it to show when I highlight this text.")
        document.getElementById('Note').style.display = 'block';
        setTimeout(function(){
        	document.getElementById('Note').style.display = 'none';
        }, 5000); // after 5 secs
    }
    
    document.onmouseup = ShowNote;
    if (!document.all) document.captureEvents(Event.MOUSEUP);
I DON'T want it to show when I highlight this text<br />
    I DO want it to show when I highlight this text.
    <div type='text' id='Note' style="display:none;" >HIDDEN DIV CONTENT</div>

变化:

  • 您需要通过“window.getSelection()”检查突出显示的内容 功能。
  • 您正在向 setTimeout 传递一个字符串
  • 隐藏不是有效的显示选项,没有

所以你知道,让文本漂浮在标签之外通常是不好的做法。因此最好将前两行放在 <p> 中标签或其他东西。

关于javascript - 在突出显示时显示 DIV,然后隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41991432/

相关文章:

javascript - Bootstrap 4 更改特定元素的工具提示颜色

javascript - 引用Javascript中的对象调用函数

javascript - 像数组一样访问json对象中的元素

javascript - 读取变量中上传的文本文件内容

javascript - 检查小数点前 4 个数字和小数点后 1 个数字

javascript - Elasticsearch 在 Jasmine 测试中没有按时索引

javascript - 动态调整进度条宽度

javascript - D3可以读取 "Geojson"文件吗?

javascript - 当路径应用程序更改时如何停止滚动事件?

Javascript:正则表达式删除 .html