简体   繁体   English

定位<img>通过 jQuery 中的 alt 属性

[英]Targeting <img> by alt attribute in jQuery

I am working on the following code.我正在处理以下代码。 I want to find out why I am not able to target the <img> tag which only has the "award logos" alt我想知道为什么我无法定位只有“奖励徽标” alt<img>标签

<img src="http://www.....jpg" alt="award logos">
<img src="http://www.....jpg" alt="Ranking">

<script>
$(document).ready(function() {
  $('img [alt="award logos"]').wrap("<a href='http://google.com' </a>");
});
</script>

Technically, what I want to do is targeting all the images that has the specific alt attributes.从技术上讲,我想要做的是针对所有具有特定 alt 属性的图像。

Remove space between img and [alt="award logos"] because of the space it is looking for child element of img with attribute [alt="award logos"] .删除img[alt="award logos"]之间的空间,因为它正在寻找具有属性[alt="award logos"]img子元素的空间。

$(document).ready(function() {
  $('img[alt="award logos"]').wrap("<a href='http://google.com'> </a>");
});

NOTE - you missed > for a (anchor) tag while wrapping img , please correct it.-你错过>a ,而包装(锚)标签img ,请更正。

JSFiddle Demo JSFiddle 演示

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

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