我要在悬停特定div时显示文本。当我将鼠标悬停时,会看到悬停时显示的文字,但它会闪烁。现在,我的文本出现在两个div(DIV 2和DIV 3)上。从div 2切换到div 3时,我得到的文字是“单击我!”。但是,如果我将光标停留在“单击我!”文本上,它将开始闪烁。
我的onMouseOver和onMouseOut函数是
function mouseOverFunc2()
{
{
var newDiv = document.createElement("span");
newDiv.setAttribute("id","id_span");
newDiv.innerText = " -- click me!";
div2.appendChild(newDiv);
console.log(div2);
}
}
function mouseOverFunc3()
{
{
var newDiv = document.createElement("span");
newDiv.setAttribute("id","id_span");
newDiv.innerText = " -- click me!";
div3.appendChild(newDiv);
console.log(div1);
}
}
var div2 = document.getElementById("div2");
var div3 = document.getElementById("div3");
div2.onmouseover = mouseOverFunc2;
div2.onmouseout = mouseOutFunc2;
div3.onmouseover = mouseOverFunc3;
div3.onmouseout = mouseOutFunc3;
function mouseOverFunc2()
{
{
var newDiv = document.createElement("span");
newDiv.setAttribute("id","id_span");
newDiv.innerText = " -- click me!";
div2.appendChild(newDiv);
console.log(div2);
}
}
function mouseOverFunc3()
{
{
var newDiv = document.createElement("span");
newDiv.setAttribute("id","id_span");
newDiv.innerText = " -- click me!";
div3.appendChild(newDiv);
console.log(div1);
}
}
function mouseOutFunc2()
{
var node = document.getElementById("id_span");
if (node.parentNode) {
node.parentNode.removeChild(node);
}
}
function mouseOutFunc3()
{
var node = document.getElementById("id_span");
if (node.parentNode) {
node.parentNode.removeChild(node);
}
}
<div id="divv" title="asda"></div>
<div id="div1">DIV 1</div>
<div id="div2">DIV 2</div>
<div id="div3">DIV 3</div>
<div id="div4">DIV 4</div>
<div id="div5">DIV 5</div>
答案 0 :(得分:1)
使用mouseenter
和mouseleave
代替mouseover
和mouseout
。不同吗?
“尽管与鼠标悬停类似,但不同之处在于它不会冒泡并且 指针移出时不会发送给任何后代 它的后代的物理空间之一变为自己的物理空间。”
var div2 = document.getElementById("div2");
var div3 = document.getElementById("div3");
div2.onmouseenter = mouseOverFunc2;
div2.onmouseleave = mouseOutFunc2;
div3.onmouseenter = mouseOverFunc3;
div3.onmouseleave = mouseOutFunc3;
function mouseOverFunc2() {
{
var newDiv = document.createElement("span");
newDiv.setAttribute("id", "id_span");
newDiv.innerText = " -- click me!";
div2.appendChild(newDiv);
console.log(div2);
}
}
function mouseOverFunc3() {
{
var newDiv = document.createElement("span");
newDiv.setAttribute("id", "id_span");
newDiv.innerText = " -- click me!";
div3.appendChild(newDiv);
console.log(div1);
}
}
function mouseOutFunc2() {
var node = document.getElementById("id_span");
if (node.parentNode) {
node.parentNode.removeChild(node);
}
}
function mouseOutFunc3() {
var node = document.getElementById("id_span");
if (node.parentNode) {
node.parentNode.removeChild(node);
}
}
<div id="divv" title="asda"></div>
<div id="div1">DIV 1</div>
<div id="div2">DIV 2</div>
<div id="div3">DIV 3</div>
<div id="div4">DIV 4</div>
<div id="div5">DIV 5</div>
答案 1 :(得分:0)