简体   繁体   English

如何使用jquery选择具有或不具有特定'alt'属性的图像

[英]How to select an image having or not having particular 'alt' attribute using jquery

I have many .png images and want to get a mentioned name where the alternative text is provided and if not mentioned in alt tag it should show 'png-image'. 我有很多.png图像,想要提供一个提到替代文本的提及名称,如果在alt标记中没有提到它应该显示'png-image'。

Below code is just putting all the png images's alternate text 'png-image'. 下面的代码只是将所有png图像的替换文字'png-image'。

Suppose I have an alt text for image ie alt="facebook" it should show alt="facebook" in place of alt="png-image" . 假设我有一个alt图像,即alt="facebook"它应该显示alt="facebook"代替alt="png-image"

Through View Source it is showing alt="facebook" in place of alt="png-image" 通过View Source,它显示alt="facebook"代替alt="png-image"

<script type="text/javascript">
    (function ($) {
        $(document).ready(function() {
            // apply Image alt attribute
            $('img[src$=".png"]').attr('alt', 'png-image');
        });
    }(jQuery));
</script>

You can use the .each method to loop over all your images with .png at the end of its src . 您可以使用.each方法在其src结尾处使用.png循环遍历所有图像。 At each image, you can then check if it has an alt attribute using: 在每个图像上,您可以使用以下命令检查它是否具有alt属性:

if(!$(this).attr('alt'))

If the image doesn't currently have an alt this if statement will run and so you can then add your own alt attribute to image. 如果图像当前没有altif语句将运行,因此您可以将自己的alt属性添加到image。

See working example below: 见下面的工作示例:

 $(document).ready(function() { $('img[src$=".png"]').each(function() { if (!$(this).attr('alt')) // check if current image tag has alt attribute $(this).attr('alt', 'png-image'); // if it doesn't add 'png-image' alt attribute }) }) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <img src="foo.png" /><br /> <img src="bar.png" alt="this is my own alt" /><br /> <img src="foo2.png" alt="this is my own alt2" /><br /> <img src="foo.png" /> 

This will work for both cases where the alt attribute is either not set, or set to nothing (ie: alt="" ) 这适用于alt属性未设置或设置为alt两种情况(即: alt=""

You can add [alt=""] selector with the current selector 您可以使用当前选择器添加[alt=""]选择器

 (function($) { $(document).ready(function() { // apply Image alt attribute $('img[src$=".png"][alt=""]').attr('alt', 'png-image'); }); }(jQuery)); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <img src='https://images.pexels.com/photos/599708/pexels-photo-599708.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500' alt='image'> <img src='https://fnetobits.s3.amazonaws.com/galleries/boyerfuneralhome/1819974/1138985594.png' alt=''> 

You can use attribute based selector in JQuery to get all the img tags not having alt attribute, 您可以在JQuery中使用基于属性的选择器来获取所有不具有alt属性的img标记,

$(document).ready(function() {
    $('img[src$=".png"][alt='']').attr('alt','png-image');
})

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

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