2014-09-01 64 views
0

我有7个使用jquery具有不同类属性的声明div。我想在鼠标悬停中使用7个div。我将如何做到这一点?如何在鼠标悬停中添加多个div(jquery)

我的7个divs是divOne,divTwo ...直到divSeven。

我有这个示例鼠标悬停代码,但只有一个div使用。

    nodeEnter.append("circle") 
        .attr("r", 30) 
        .style("stroke","white") 
        .on("mouseover", function(d) { 
         divOne.transition() 
          .duration(200) 
          .style("opacity", .9); 
         divOne.html(
          "Name" + "<br />" + "Address" 
         ) 
          .style("left", (d3.event.pageX) + "px") 
          .style("top", (d3.event.pageY - 28) + "px"); 
         }) 
         .on("mouseout", function(d) { 
         divOne.transition() 
          .duration(500) 
          .style("opacity", 0); 
         }); 

如何在鼠标悬停期间添加其他div?任何帮助。谢谢

+0

您可以创建一个HTML小提琴和JS并把它添加到您的文章吗? – retrovertigo 2014-09-01 05:06:41

+1

您的意思是*添加多个div *,您想要在鼠标悬停上绑定多个div或在鼠标悬停上添加多个div。 – Khaleel 2014-09-01 05:06:48

+0

这是你在找什么? http://stackoverflow.com/questions/17246650/how-to-create-a-jquery-mouseover-for-multiple-divs-by-id – 2014-09-01 05:10:02

回答

0

你需要有这样的东西。

nodeEnter.append("circle") 
     .attr("r", 30) 
     .style("stroke", "white") 
     .on("mouseover", function(d) { 
      onMouseOver(); 
     }) 
     .on("mouseout", function(d) { 
      onMouseOut(); 
     }); 

var divElements = $('.classofdiv1', '.classofdiv2'......); //add all the div's class 

function onMouseOver() { 
    $(divElements).each(function(index) { 
     $(this).transition() 
      .duration(200) 
      .style("opacity", .9); 
     $(this).html(
       "Name" + "<br />" + "Address" 
      ) 
      .style("left", (d3.event.pageX) + "px") 
      .style("top", (d3.event.pageY - 28) + "px"); 

    }); 
} 

function onMouseOut() { 
    $(divElements).each(function(index) { 
     $(this).transition() 
      .duration(500) 
      .style("opacity", 0); 

    }); 
} 

希望这有助于:)

+0

你好。我想问一下divElements,它是我的div的名称还是它的类属性? – Kentarou 2014-09-01 05:56:22

+0

它是类属性。你也可以使用'document.getElementsByClassName('nameOfClassHere');'如果同一个类适用于所有div – Khaleel 2014-09-01 06:00:11

+0

好的谢谢。我更喜欢使用上面的解决方案,因为我的div有不同的类。 – Kentarou 2014-09-01 06:22:24