简体   繁体   English

自动在图片上添加替代项

[英]Auto add alt on images

When I add 当我添加

$(document).ready(function() {  
$('#readerareaimg img').each(function(){
var filename = $img.attr('src')
$img.attr('title', filename.substring((filename.lastIndexOf('/'))+1, filename.lastIndexOf('.')));
$img.attr('alt', filename.substring((filename.lastIndexOf('/'))+1, filename.lastIndexOf('.')));
});  

My website will generate alt and title tags automatically on images based on their file name, but I want to add any word before the file name. 我的网站将根据图像的文件名自动在图像上生成alt和title标签,但是我想在文件名前添加任何单词。 For example file name is comic.jpg and I will display alt="comic" , but i want that alt to become alt="read comic" . 例如,文件名是comic.jpg,我将显示alt="comic" ,但我希望该alt成为alt="read comic"

Can someone explain how to make this? 有人可以解释如何做到这一点吗? Please, sir :3 请先生:3

It's a simple case of concatenating the string when you set the alt attribute: 这是设置alt属性时连接字符串的简单情况:

$img.attr('alt', 'read ' + filename.substring((filename.lastIndexOf('/'))+1, filename.lastIndexOf('.')));

As for the number request from the comments, you can capture the index argument which is zero based, then +1 it to get 1-n. 至于注释中的数字请求,您可以捕获从零开始的index参数,然后对其进行+1以获得1-n。

$('#readerareaimg img').each(function(index){
    $(this).attr('alt', 'comic naruto image ' + (index+1));
});  

You can use '+' sign in the where you put value of attribute. 您可以在属性值的放置位置使用“ +”号。

$img.attr('alt', 'required word'+filename.substring((filename.lastIndexOf('/'))+1, filename.lastIndexOf('.'))); 

or 要么

$img.attr('alt',filename.substring((filename.lastIndexOf('/'))+1, filename.lastIndexOf('.'))+'required word');

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

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