简体   繁体   English

如何检查图像源中是否包含某个单词并替换该单词?

[英]How can I check if a word is in an image source, and replace that word?

I would like to replace the words in an image link based on if that word exists in it.我想根据该词是否存在来替换图像链接中的词。 My code seems like it would work but is doesn't.我的代码似乎可以工作,但不能。 Heres my code:这是我的代码:

 topimg.addEventListener('error', function(){

    if(topimg.src.indexOf('Tee')){
        topimg.src.replace('Tee', 'T-Shirt');
    }

From the MDN web docs for String.prototype.replace() :来自String.prototype.replace()MDN 网络文档

The replace() method returns a new string with some or all matches of a pattern replaced by a replacement. replace() 方法返回一个新字符串,其中部分或全部模式匹配项被替换项替换。 ... ...

The original string is left unchanged.原始字符串保持不变。

So therefore try:因此,请尝试:

 topimg.addEventListener('error', function() {
    if (topimg.src.indexOf('Tee')) {
        topimg.src = topimg.src.replace('Tee', 'T-Shirt');
    }
    ...
 }

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

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