查找img是否在jQuery中具有alt,然后从数组中添加 [英] find if a img have alt in jquery if not then add from array

查看:159
本文介绍了查找img是否在jQuery中具有alt,然后从数组中添加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先需要查找网站中的所有img

first need to find all img in the sites

$("#body").find(img)

,然后检查img是否具有"alt"属性,如果图像具有该属性,则将转义该图像,如果图像没有该属性,或者alt为空,则会从列表或数组.

and then check if the img have the "alt" attribute, if image have the attribute it'll be escaped and if it not have one or the alt is empty,a string will be randomly added to img from a list or array.

我现在必须在函数中使用.each()的某种方式,但是我对jquery还是陌生的,所以一点点帮助将是完美的.

I now that i have to use .each() some how in a function but I'm new to jquery so a little bit help would be perfect.

推荐答案

var arr = ['hello', 'hi', 'yo'];
$('#body img').each(function() {
    if ( ! $(this).attr('alt'))
        $(this).attr('alt', arr[Math.round(Math.random() * (arr.length - 1)))]);
});

这篇关于查找img是否在jQuery中具有alt,然后从数组中添加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆