使用 jQuery 获取特定图像元素的来源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10598861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:34:49  来源:igfitidea点击:

Getting the source of a specific image element with jQuery

jqueryjquery-selectors

提问by Irakli Lekishvili

I have many image elements and want to get a specific image's source where the alternative text is "example".

我有很多图像元素,想要获取特定图像的来源,其中替代文本是"example"

I tried this:

我试过这个:

var src = $('.conversation_img').attr('src');

but I can't get the one I need with that.

但我无法得到我需要的那个。

How do I select a specific image element based on its alternative text?

如何根据替代文本选择特定图像元素?

回答by Murtaza

To select and element where you know only the attribute value you can use the below jQuery script

要选择您只知道属性值的元素,您可以使用以下 jQuery 脚本

var src = $('.conversation_img[alt="example"]').attr('src');

Please refer the jQuery Documentationfor attribute equals selectors

请参阅jQuery 文档了解属性等于选择器

Please also refer to the example in Demo

另请参考Demo中的例子

Following is the code incase you are not able to access the demo..

以下是代码,以防您无法访问演示..

HTML

HTML

<div>
    <img alt="example" src="\images\show.jpg" />
    <img  alt="exampleAll" src="\images\showAll.jpg" />  

</div>

SCRIPT JQUERY

脚本查询

var src = $('img[alt="example"]').attr('src');
alert("source of image with alternate text = example - " + src);


var srcAll = $('img[alt="exampleAll"]').attr('src');
alert("source of image with alternate text = exampleAll - " + srcAll );

Output will be

输出将是

Two Alert messages each having values

两条警报消息,每条消息都有值

  1. source of image with alternate text = example - \images\show.jpg
  2. source of image with alternate text = exampleAll - \images\showAll.jpg
  1. 带有替代文本的图像源 = 示例 - \images\show.jpg
  2. 带有替代文本的图像源 = exampleAll - \images\showAll.jpg

回答by strah

$('img.conversation_img[alt="example"]')
    .each(function(){
         alert($(this).attr('src'))
    });

This will display src attributes of all images of class 'conversation_img' with alt='example'

这将使用 alt='example' 显示类 'conversation_img' 的所有图像的 src 属性

回答by ThiefMaster

var src = $('img.conversation_img[alt="example"]').attr('src');

If you have multiple matching elements only the src of the first one will be returned.

如果您有多个匹配的元素,则只会返回第一个的 src。

回答by Wonx2150

If you do not specifically need the alt text of an image, then you can just target the class/id of the image.

如果您不是特别需要图像的替代文本,那么您可以只定位图像的类/ID。

$('img.propImg').each(function(){ 
     enter code here
}

I know it's not quite answering the question, though I'd spent ages trying to figure this out and this question gave me the solution :). In my case I needed to hide any image tags with a specific src.

我知道这并不能完全回答这个问题,尽管我花了很长时间试图弄清楚这一点,而这个问题给了我解决方案:)。在我的情况下,我需要隐藏具有特定 src 的任何图像标签。

$('img.propImg').each(function(){ //for each loop that gets all the images. 
        if($(this).attr('src') == "img/{{images}}") { // if the src matches this
        $(this).css("display", "none") // hide the image.
    }
});