简体   繁体   English

jQuery悬停获取标题属性并显示Div

[英]Jquery Hover Take Title Attribute and Display Div

After tinkering with this for hours I am hoping someone can shed some insight on this. 修改了几个小时后,我希望有人可以对此有所了解。

I have 2 divs side by side. 我有2个div并排。 On the right side there is a list of links with the containing div id=list. 在右侧有一个链接列表,其中包含div id = list。 On the left side there the viewarea div that will display a picture and description of the link on hover. 在左侧的viewarea div将显示图片和悬停链接的说明。 So When you hover over a link it should display a div on the left side that is specific for that div. 因此,当您将鼠标悬停在链接上时,它应该在左侧显示该div专用的div。 However, I want to add a bit of automation to this. 但是,我想为此增加一些自动化。

Basically on hover over a link in the list div, will toggle the display of the div on the left to display block. 基本上,将鼠标悬停在列表div中的链接上时,将在左侧切换div的显示以显示块。 The hidden div id that will display the picture and description are named the same as the title attribute of the link in the list div. 将显示图片和描述的隐藏div ID与列表div中的链接的title属性相同。 So on hover the link should then toggle the display of a div with the id equal to the title to display: block. 因此,在悬停时,链接应然后切换ID为等于display:block标题的div的显示。

So Basically my scripts logic would be something like this: 所以基本上我的脚本逻辑是这样的:

On hover over link in list div. 将鼠标悬停在列表div中的链接上时。 Get title attribute for this link. 获取此链接的标题属性。 Change div with id equal to the title attribute gotten above (in #viewarea) to display block. 更改ID等于上面(在#viewarea中)获得的title属性的div以显示块。

so when hovering over the monkeys link it will take the title “monkeys” and toggle the visibility of the div id=”monkeys” in the viewarea div. 因此,当将鼠标悬停在猴子链接上时,它将以标题“ monkeys”命名,并在viewdiv div中切换div id =“ monkeys”的可见性。

Here is rough html/css code: 这是大致的html / css代码:

#list {
width: 480px;
float: right;
}

#viewarea {
width: 480px;
}

<div id="list">

<h1>
Animals
</h1>
<p>
<a href="monkeys_link"  title="monkeys">Monkeys</a>
</p>
<p>
<a href=”kittens_link" title="kittens">Kittens</a>
</p>
<p>
<a href="pigs_link" title="pigs">Pigs</a>
</p>

</div>

<div id="viewarea">

<div id="monkeys" style="display: none;">
<img src="picture_of_monkeys.jpg"></img>
Summary text
</div>

<div id="kittens" style="display: none;">
<img src="picture_of_kittens.jpg"></img>
Summary text
</div>

<div id="pigs" style="display: none;">
<img src="picture_of_pigs.jpg"></img>
Summary text
</div>

</div>

This is what i ended up for the external jquery script. 这就是我最终为外部jquery脚本编写的内容。 I know I need some sort of looping construct like .each but I have not been able to get it working. 我知道我需要某种循环结构,例如.each,但是我无法使其工作。 Am I on the right track? 我在正确的轨道上吗?

$(document).ready(function() {

var title1 = $("#list a").attr("title");


$("#list a").hover(
function () {
$("#" + "title1").css("display", "block");
},
function () {
$("#" + "title1").css("display", "none");
}
);

});

Sorry for the lengthy post, thanks so much. 抱歉,冗长的帖子,非常感谢。

$("#list")
  // On mouseenter and mouseleave, we want to react
  .on("mouseenter mouseleave", "a", function(e){
    // Find the element whose id matches our current title
    var el = $("#" + this.title);
    // Determine if we're entering or exiting the link
    switch ( e.type ) {
      // Show the element or hide the element
      case "mouseenter" : el.show(); break;
      case "mouseleave" : el.hide();
    }
  });

Demo: http://jsbin.com/amitet/2/edit 演示: http//jsbin.com/amitet/2/edit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM