[英]How to select an image having particular 'alt' attribute using jquery
I have the below html in my page . 我的页面中有以下html。
<div id="bb-bookblock" class="bb-bookblock bb-vertical" style="height: 578.24885475px">
<div class="bb-item" style="display: none;">
<a href="#">
<img src="photorepository/admin/a-123/14/31 copy_200comp.jpg" alt="31 copy_200comp.jpg" style="height:100%">
</a>
</div>
<div class="bb-item" style="display: none;">
<a href="#"><img src="photorepository/admin/a-123/14/32 copy_200comp.jpg" alt="32 copy_200comp.jpg" style="height:100%"></a>
</div>
<div class="bb-item" style="display: none;">
<a href="#"><img src="photorepository/admin/a-123/14/4 copy_200comp.jpg" alt="4 copy_200comp.jpg" style="height:100%"></a>
</div>
<div class="bb-item" style="display: none;">
<a href="#"><img src="photorepository/admin/a-123/14/5 copy_200comp.jpg" alt="5 copy_200comp.jpg" style="height:100%"></a>
</div>
</div>
I need to turn style="display: block;"
我需要转换style="display: block;"
of the div having class bb-item
of the image having a particular alt
. 具有特定alt
的图像的类bb-item
的div。 For example if the alt
is '5 copy_200comp.jpg'
, then the parent div of that particular image turns like this: 例如,如果alt
是'5 copy_200comp.jpg'
,那么该特定图像的父div将如下所示:
<div class="bb-item" style="display: block;"> <a href="#"><img src="photorepository/admin/a-123/14/5 copy_200comp.jpg" alt="5 copy_200comp.jpg" style="height:100%"></a></div>
I tried var src = $('img[alt="example"]')
and similar constructs but they aren't working. 我尝试了var src = $('img[alt="example"]')
和类似的结构,但它们不起作用。
use .attr("element")
.Your edit question have so many img so use .each()
for check all alt 使用.attr("element")
编辑问题有这么多的img所以使用.each()
来检查所有alt
$('img').each(function(){
if($(this).attr("alt") == "5 copy_200comp.jpg"){
$(this).closest(".bb-item").css("display","block");
}
});
You could use :has()
selector as follow. 你可以使用:has()
选择器如下。
$('.bb-item:has(img[alt="5 copy_200comp.jpg"])').show();
The selector .bb-item:has(img[alt="5 copy_200comp.jpg"])
will select the element having class bb-item
having an image inside it with the specified alt
attribute value. 选择器.bb-item:has(img[alt="5 copy_200comp.jpg"])
将选择具有类bb-item
的元素,其中包含具有指定alt
属性值的图像。
Here's a snippet to add display:block
on .bb-item
if the img inside has alt="example"
这里有一个片段来添加display:block
on .bb-item
如果img里面有alt="example"
if($("img").attr("alt") == "example"){ $("img").parents(".bb-item").show(); }
.bb-item{ display:none; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="bb-item" > <a href="#"><img src="photorepository/admin/a-123/14/5 copy_200comp.jpg" alt="example" style="height:100%"></a></div>
Let do some functions for resolve this problem. 让我们做一些函数来解决这个问题。 Which I think with the function name is self explanatory. 我认为函数名称是自解释的。 You can check the example running here http://jsbin.com/nicuxoriqu/edit?html,js,output Notice that is displaying the correct container from the alt
attribute of the image. 您可以查看此处运行的示例http://jsbin.com/nicuxoriqu/edit?html,js,output请注意,该图像的alt
属性显示正确的容器。
For find the image we will use the CSS attribute selector [attr=value]
which match exactly with the value and then we will find the .bb-item
closer container to that image. 为了找到图像,我们将使用与该值完全匹配的CSS属性选择器[attr=value]
,然后我们将找到该图像的.bb-item
更接近的容器。
function findImageInsideBookBlockByAlt(alt) {
// Find the image inside the .bb-bookblock element with specific alt
return $('.bb-bookblock img[alt="' + alt + '"]');
}
function findBBItemFromImage($image) {
// Find the closest element from the parents node that have the class .bb-item
return $image.closest('.bb-item');
}
var $image = findImageInsideBookBlockByAlt('4 copy_200comp.jpg');
var $BBItem = findBBItemFromImage($image);
// Do whatever you want with the $BBItem now
// From your comment, just do this
$BBItem.show();
It is working fine. 它工作正常。 I just select and display the src of alt attribute selector. 我只是选择并显示alt属性选择器的src。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"> </script> <div class="bb-item" style="display: block;"> <a href="#"><img src="photorepository/admin/a-123/14/5 copy_200comp.jpg" alt="5 copy_200comp.jpg" style="height:100%"></a></div> <script> $(document).ready(function(){ alert($('img[alt="5 copy_200comp.jpg"]').attr("src")); }); </script>
This code will work. 这段代码可行。 Please check: 请检查:
<div id="bb-bookblock" class="bb-bookblock bb-vertical" style="height: 578.24885475px">
<div class="bb-item" style="display: none;">
<a href="#"><img src="photorepository/admin/a-123/14/31 copy_200comp.jpg" alt="31 copy_200comp.jpg" style="height:100%"></a>
</div>
<div class="bb-item" style="display: none;">
<a href="#"><img src="photorepository/admin/a-123/14/32 copy_200comp.jpg" alt="32 copy_200comp.jpg" style="height:100%"></a>
</div>
<div class="bb-item" style="display: none;">
<a href="#"><img src="photorepository/admin/a-123/14/4 copy_200comp.jpg" alt="4 copy_200comp.jpg" style="height:100%"></a>
</div>
<div class="bb-item" style="display: none;">
<a href="#"><img src="photorepository/admin/a-123/14/5 copy_200comp.jpg" alt="5 copy_200comp.jpg" style="height:100%"></a>
</div>
</div>
<script type="text/javascript">
$(document).ready(function() {
$( ".bb-item a" ).each(function() {
if($($( this ).html()).attr("alt")=='5 copy_200comp.jpg'){
$(this).parent('div').show();
}
});
});
</script>
You can try it by this logic, if any image has alt
exists in it's src
, and any of his parents
has bb-item
class, then show it's parents: 您可以通过此逻辑尝试它,如果任何图像在其src
存在alt
,并且他的任何parents
都有bb-item
类,则显示它的父母:
var img = $('#bb-bookblock img');
img.each(function (index, image) {
var imgSrc = $(image).attr('src');
var imgAlt = $(image).attr('alt');
if ( imgSrc.indexOf(imgAlt) != -1 ) {
if ( $(image).parents().hasClass('bb-item') ) {
$(image).parents().css('display', 'block');
}
}
});
https://jsfiddle.net/c37ostsc/8/ https://jsfiddle.net/c37ostsc/8/
$(document).ready( function () { 'use strict'; function setAltToSrc ( settedAlt ) { $('#bb-bookblock img').each(function ( index, image ) { var $image = $(image); var imgSrc = $image.attr('src'); var imgAlt = $image.attr('alt'); if ( imgAlt === settedAlt ) { $image.attr( 'src', imgSrc + imgAlt ); } imgSrc = $image.attr('src'); // To store the new src if ( imgSrc.indexOf(imgAlt) != -1 ) { if ( $image.parents().hasClass('bb-item') ) { $image.parents().css( 'display', 'block' ); } } }); } setAltToSrc('/9b59b6'); $('p').text( $('img').eq(2).attr('src') ); });
p { position: absolute; top: 150px; left: 0; z-index: 88888; color: #666 } img { width: 100px !important; height: 100px !important; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="bb-bookblock" class="bb-bookblock bb-vertical" style="height: 578.24885475px"> <div class="bb-item" style="display: none;"> <a href="#"> <img src="http://placehold.it/350x150" alt="/2ecc71"> </a> </div> <div class="bb-item" style="display: none;"> <a href="#"> <img src="http://placehold.it/350x150" alt="/9b59b6"> </a> </div> <div class="bb-item" style="display: none;"> <a href="#"> <img src="http://placehold.it/350x150" alt="/3498db"> </a> </div> <div class="bb-item" style="display: none;"> <a href="#"> <img src="http://placehold.it/350x150" alt="/ecf0f1"> </a> </div> </div> <p> </p>
By using code organization . 通过使用代码组织。
<div class="main">
<div id="bb-bookblock" class="bb-bookblock bb-vertical" style="height: 578.24885475px">
<div class="bb-item" style="display: none;">
<a href="#">
<img src="photorepository/admin/a-123/14/31 copy_200comp.jpg" alt="31 copy_200comp.jpg" style="height:100%">
</a>
</div>
<div class="bb-item" style="display: none;">
<a href="#"><img src="photorepository/admin/a-123/14/32 copy_200comp.jpg" alt="32 copy_200comp.jpg" style="height:100%"></a>
</div>
<div class="bb-item" style="display: none;">
<a href="#"><img src="photorepository/admin/a-123/14/4 copy_200comp.jpg" alt="4 copy_200comp.jpg" style="height:100%"></a>
</div>
<div class="bb-item" style="display: none;">
<a href="#"><img src="photorepository/admin/a-123/14/5 copy_200comp.jpg" alt="5 copy_200comp.jpg" style="height:100%"></a>
</div>
</div>
</div>
<script type="text/javascript">
(function($, window, document, undefined)
{
var
constants = {
wrapper: '.main',
sub: '.bb-item a img',
hiddenDiv: '.bb-item'
},
properties = {
wrapper: null,
sub :null,
hiddenDiv :null
},
methods = {
init: function ()
{
properties.wrapper = $(constants.wrapper);
properties.sub = properties.wrapper.find(constants.sub);
properties.hiddenDiv = properties.wrapper.find(constants.hiddenDiv);
properties.hiddenDiv.css({
display: 'none'
});
properties.sub
.each( methods.pickAlt );
},
pickAlt: function (event)
{
var $this = $(this);
var needToShow = $this.attr('alt');
if(needToShow == '5 copy_200comp.jpg'){
methods.show($this);
}
},
show: function (event)
{
var $this = event;
$this.parent().parent().css({
display: 'block'
});
}
};
$(document).ready(methods.init);
})(jQuery, window, document);
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.