当鼠标悬停在文本中的单词上时显示警报 [英] display alert when mouse hovers over word in text
本文介绍了当鼠标悬停在文本中的单词上时显示警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我几天都在努力解决这个问题。我需要有人引导我朝着正确的方向前进。我一直在网上搜索。我不确定我是否采取了正确的方法。我需要的是,每当一个人悬停在特定关键字上时,它应该显示一个警告框。在这个例子中,单词是else。当我运行代码时,它不会给出任何错误,并且当鼠标悬停在单词上时不会显示任何内容。
I have been struggling with this for a few days. I need somebody to steer me in the right direction. I have been searching on the web. I am not sure if I took the right approach. What I need is that each time a person hovers over a particular keyword, it should display an alert box. In this example the word is else. When I run the code it does not give any errors and does not display anything when mouse hovers on the word.
function on_func2()
{
var searchString = 'else';
var elements = document.getElementById('paragraph2');
for (var i = 0; i < elements.length; i++)
{
if (elements[i].innerHTML.indexOf(searchString) !== -1)
{
alert('Match');
break;
}
}
}
推荐答案
使用jQuery 刻字插件
Using jQuery lettering plugin
<p class="word_split">if you were not there, else I would not have won.<p>
$(".word_split").lettering('words');
$('.word_split').mouseover(function(event) {
var word=event.target.innerHTML;
if (word == "else")
alert("Yep");
});
这是一个演示: jsFiddle
这篇关于当鼠标悬停在文本中的单词上时显示警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文