如何在JavaScript中从IMG标记获取ALT

如何在JavaScript中从IMG标记获取ALT,javascript,jquery,html,wordpress,lightbox,Javascript,Jquery,Html,Wordpress,Lightbox,我正在使用脚本在lightbox中显示我的图库。我使用了一些插件,但它们都没有显示alt=“”(alt添加在wordpress的媒体中) 如何修改下面链接中的代码以显示alt属性 我在代码中找到了一些东西,但我不知道如何将动态alt放在那里(在代码中用大写文本注释)。动态我的意思是,当用户在wordpress仪表板中添加img时,他会在那个时候放置alt { key: '_preloadImage', value: function _preloa

我正在使用脚本在lightbox中显示我的图库。我使用了一些插件,但它们都没有显示alt=“”(alt添加在wordpress的媒体中)

如何修改下面链接中的代码以显示alt属性

我在代码中找到了一些东西,但我不知道如何将动态alt放在那里(在代码中用大写文本注释)。动态我的意思是,当用户在wordpress仪表板中添加img时,他会在那个时候放置alt

{
            key: '_preloadImage',
            value: function _preloadImage(src, $containerForImage) {
                var _this4 = this;

                $containerForImage = $containerForImage || false;

                var img = new Image();
                if ($containerForImage) {
                    (function () {

                        // if loading takes > 200ms show a loader
                        var loadingTimeout = setTimeout(function () {
                            $containerForImage.append(_this4._config.loadingMessage);
                        }, 200);

                        img.onload = function () {
                            if (loadingTimeout) clearTimeout(loadingTimeout);
                            loadingTimeout = null;
                            var image = $('<img />');
                            image.attr('src', img.src);
                            image.addClass('img-fluid');
image.attr('alt',"Temp TEXT"); // HERE I WOULD LIKE TO DISPLAY THE ALT AUTOMATICALLY - NOT STATIC AS IT IS NOW

                            // backward compatibility for bootstrap v3
                            image.css('width', '100%');

                            $containerForImage.html(image);
                            if (_this4._$modalArrows) _this4._$modalArrows.css('display', ''); // remove display to default to css property

                            _this4._resize(img.width, img.height);
                            _this4._toggleLoading(false);
                            return _this4._config.onContentLoaded.call(_this4);
                        };
                        img.onerror = function () {
                            _this4._toggleLoading(false);
                            return _this4._error(_this4._config.strings.fail + ('  ' + src));
                        };
                    })();
                }

                img.src = src;
                return img;
            }
        },
{
键:''u preload image',
值:函数_preloadImage(src,$containerForImage){
var _this4=此;
$containerForImage=$containerForImage | | false;
var img=新图像();
if($containerForImage){
(功能(){
//如果加载时间>200ms,则显示加载程序
var loadingTimeout=setTimeout(函数(){
$containerForImage.append(_this4._config.loadingMessage);
}, 200);
img.onload=函数(){
if(loadingTimeout)清除超时(loadingTimeout);
loadingTimeout=null;
var image=$('

我在我的页面上使用wordpress,我的JS技能很差,所以我想问你:)。

我们真的不知道你的
img
变量的类型。如果你想将
image
的alt标签设置为
img
中的一个,只需执行以下操作:

image.attr('alt', img.attr('alt'));
…如果它是jQuery对象。否则,如果
img
是纯JavaScript,则可以执行以下操作:

image.attr('alt', img.alt);

如果
img
对象保持现有的alt,并且
image
是新的jQuery对象,则使用jQuery设置它

image.attr('alt', img.getAttribute('alt'))

所以像对待src一样阅读alt?我正试图这样做…
functions.js?ver=4.9.3:874 Uncaught TypeError:$(…).ekkoLightbox不是htmlanchoreElement的函数。(functions.js?ver=4.9.3:874)在HTMLDocument.dispatch(jquery.js?ver=1.12.4:3)在HTMLDocument.r.handle(jquery.js?ver=1.12.4:3)(匿名)@functions.js?ver=4.9.3:874 dispatch@jquery.js?ver=1.12.4:3 r.handle@jquery.js?ver=1.12.4:3
我对您的解决方案有一个错误
ekko lightbox.js?ver=4.9.3:549未捕获类型错误:img.attr不是Image.img.onload(ekko lightbox.js?ver=4.9.3:549)上的函数img.onload@ekko lightbox.js?ver=4.9.3:549
如果您尝试
image.attr('alt',this.alt);
image.attr('alt',$(this.attr('alt'));
?第二个没有显示alt,但是第一个显示空的
alt=”“
。我已经输入了
console.log(这个);
并且它抛出了
。没有alt,但我肯定在添加img时在wordpress中添加了它。不工作
未捕获类型错误:image.setAttribute不是image.img.onload(ekko lightbox.js?ver=4.9.3:549)img.onload@ekko lightbox.js?ver=4.9.3:549的函数