将文本添加到图像的src

时间:2023-01-02 07:33:33

Hi I need a jQuery function to add a text to src of all images in the page. for example after one minute of load of my page I want to add "mobileserver" to src of all images like this.

嗨,我需要一个jQuery函数来向页面中的所有图像的src添加文本。例如,在我的页面加载一分钟之后,我想将“mobileserver”添加到像这样的所有图像的src中。

<img src="mobileserver/images/sampleimage.jpg" />

2 个解决方案

#1


JQUERY ANSWER
As Wolff stated there no point in using setInterval instead of setTimeout

JQUERY ANSWER正如Wolff所说,使用setInterval而不是setTimeout是没有意义的

$(document).ready(function () {
    setTimeout(function () {
        $('img').each(function () {
            var nSrc= 'mobileserver/' + $(this).attr('src');
            $(this).attr('src', nSrc);
        });
    }, 60000);
});



EDIT
Since my answer wasn't fully correct, I'm trying to deserve these points, so here it is a pure javascript solution:

编辑因为我的答案不完全正确,我试图得到这些点,所以这里是一个纯粹的JavaScript解决方案:

(function () {
    setTimeout(function () {
        var imgTag = document.getElementsByTagName("img"),
            count = imgTag.length;
        for (var i = 0; i < count; i++) {
            imgTag[i].setAttribute("src", 'mobileserver/' + imgTag[i].getAttribute('src'));
        }
    }, 60000);
})();

The only problem could be the browser compatibility, check this answer for other method to check if the document is ready:https://*.com/a/9899701/1139052

唯一的问题可能是浏览器兼容性,检查此答案是否有其他方法来检查文档是否准备就绪:https://*.com/a/9899701/1139052

#2


You can use setTimeout() to set delay and attr() with callback function for updating the src attribute,

您可以使用setTimeout()设置延迟和attr()以及用于更新src属性的回调函数,

$(document).ready(function () {
   setTimeout(function () {
        $('img').attr('src',function (i,v) {
            return 'mobileserver/' + v;
        });
    }, 60000);
});

#1


JQUERY ANSWER
As Wolff stated there no point in using setInterval instead of setTimeout

JQUERY ANSWER正如Wolff所说,使用setInterval而不是setTimeout是没有意义的

$(document).ready(function () {
    setTimeout(function () {
        $('img').each(function () {
            var nSrc= 'mobileserver/' + $(this).attr('src');
            $(this).attr('src', nSrc);
        });
    }, 60000);
});



EDIT
Since my answer wasn't fully correct, I'm trying to deserve these points, so here it is a pure javascript solution:

编辑因为我的答案不完全正确,我试图得到这些点,所以这里是一个纯粹的JavaScript解决方案:

(function () {
    setTimeout(function () {
        var imgTag = document.getElementsByTagName("img"),
            count = imgTag.length;
        for (var i = 0; i < count; i++) {
            imgTag[i].setAttribute("src", 'mobileserver/' + imgTag[i].getAttribute('src'));
        }
    }, 60000);
})();

The only problem could be the browser compatibility, check this answer for other method to check if the document is ready:https://*.com/a/9899701/1139052

唯一的问题可能是浏览器兼容性,检查此答案是否有其他方法来检查文档是否准备就绪:https://*.com/a/9899701/1139052

#2


You can use setTimeout() to set delay and attr() with callback function for updating the src attribute,

您可以使用setTimeout()设置延迟和attr()以及用于更新src属性的回调函数,

$(document).ready(function () {
   setTimeout(function () {
        $('img').attr('src',function (i,v) {
            return 'mobileserver/' + v;
        });
    }, 60000);
});