简体   繁体   English

jQuery显示/隐藏具有匹配标题和alt标签的元素

[英]JQuery show/hide elements with matching title and alt tag

Is it possible to show an image via mouseover on a link whose only matching tags are title and alt? 是否可以通过鼠标悬停在仅匹配标签为title和alt的链接上显示图像?

My CMS can only generate matching title and alt tags for 2 completely seperated elements. 我的CMS只能为2个完全分开的元素生成匹配的标题和alt标签。 It roughly looks like this: 大致如下所示:

<a href="#" title="aubergine">hover this to show image</a>

<img width="300" height="300" alt="aubergine" src="http://i.imgur.com/0MmkDiI.jpg"></img>

I've been able to target the img like so: 我已经能够像这样定位img:

$('a').mouseover(function() {
$('[alt="aubergine"]').css( "opacity", "100" );
});

But instead of the specific target [alt="aubergine"] i would have to get the image via [alt="title of this link i just hovered"] . 但是,除了特定的目标[alt="aubergine"]我还必须通过[alt="title of this link i just hovered"] [alt="aubergine"]来获取图像。

Here is a fiddle with the working prototype so far: https://jsfiddle.net/82xnqu6j/1/ 这是到目前为止工作中的原型的小提琴: https : //jsfiddle.net/82xnqu6j/1/

Use jQuery to pull out the title attribute of your current element, like in the following live example. 使用jQuery提取当前元素的title属性,如下面的实时示例所示。

That way you can generalize it for all matching links. 这样,您可以将其推广到所有匹配的链接。

Live Example: 现场示例:

 $('a').mouseover(function() { $('[alt="' + $(this).attr("title") + '"]').css( "opacity", "1" ); }); $('a').mouseout(function() { $('[alt="' + $(this).attr("title") + '"]').css( "opacity", "0" ); }); 
 img {opacity:0;} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a href="#" title="aubergine">hover this to show image</a> <img width="100" height="100" alt="aubergine" src="http://i.imgur.com/0MmkDiI.jpg"></img> 

JSFiddle Version: https://jsfiddle.net/82xnqu6j/4/ JSFiddle版本: https ://jsfiddle.net/82xnqu6j/4/

Simply try getting the actual title from the link and using it as part of the selector 只需尝试从链接获取实际标题并将其用作选择器的一部分

    $('a').mouseover(function() { 
        $('[alt="'+this.title+'"]').css( "opacity", "100" );
    });

This applies to any attribute, such as title, src, href, etc. 这适用于任何属性,例如标题,src,href等。

Check the w3schools page for this in: http://www.w3schools.com/cssref/sel_attribute_value.asp 在以下网址检查w3schools页面: http : //www.w3schools.com/cssref/sel_attribute_value.asp

You can just use $(this).attr('title') to choose any attribution of the element in jQuery, same as this.title in native JavaScript. 您可以只使用$(this).attr('title')来选择jQuery中元素的任何属性,与本机JavaScript中的this.title相同。

 $('a').mouseover(function() { $('[alt="' + $(this).attr('title') + '"]').css("opacity", "100"); }); $('a').mouseout(function() { $('[alt="' + $(this).attr('title') + '"]').css("opacity", "0"); }); 
 img { opacity: 0 } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a href="#" title="aubergine">hover this to show image</a> <a href="#" title="test">hover this to show image</a> <img width="300" height="300" alt="aubergine" src="http://i.imgur.com/0MmkDiI.jpg"></img> <img width="300" height="300" alt="test" src="http://i.stack.imgur.com/gVqLo.png"></img> 

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

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