Javascript 如何使用jQuery设置图像src

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2183347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 23:08:38  来源:igfitidea点击:

How to set the image src using jQuery

javascriptjqueryimagesrc

提问by Rakesh Juyal

I am trying to change the image src attribute using jQuery

我正在尝试使用 jQuery 更改图像 src 属性

jQuery("#imageID").attr('src','http://localhost:8080/images/1/myImage.png' );

using the above code i can change the src attribute, but when i try this:-

使用上面的代码我可以更改 src 属性,但是当我尝试这样做时:-

jQuery("#imageID").attr('src',jQuery("#imageBlock").css('background-image') );

i am unable to change the src.

我无法更改 src。

provided

假如

alert ( jQuery("#imageBlock").css('background-image') );

returns:

返回:

url(http://localhost:8080/images/1/myImage.png)

网址(http://localhost:8080/images/1/myImage.png

Edit #1Just when i was about to accept the solution. I must say, almost all solution worked in FF. I tried:

编辑 #1就在我即将接受解决方案时。我必须说,几乎所有的解决方案都适用于 FF。我试过:

  • slice(4,-1);
  • split("(")[1] > then replace ( ")" , "" );
  • 切片(4,-1);
  • split("(")[1] > 然后替换 (")" , "" );

I guess others will also work. But none of the solutions is working in IE. Reason being: while FF returns:

我想其他人也会工作。但是没有一个解决方案在 IE 中有效。原因是:当 FF 返回时:

url(http://localhost:8080/images/1/myImage.png)

网址(http://localhost:8080/images/1/myImage.png

IE Returns:

IE 返回:

url("http://localhost:8080/images/1/myImage.png")

url(" http://localhost:8080/images/1/myImage.png")

^^ mind the quotes here

^^ 请注意这里的引号

Now, what could be the generic way to set the src attr. Do i need to test the browser if it is IE or not?

现在,设置 src attr 的通用方法是什么?是否需要测试浏览器是否是 IE?

This is the working code.

这是工作代码。

var src = "";
    if ( jQuery.browser.msie ) {
        src = jQuery("#imageBlock").css('background-image').slice(5,-2);        
    }else{
        src = jQuery("#imageBlock").css('background-image').slice(4,-1);
    }   
    jQuery("#imageID").attr('src', src );

I really don't like it :x. If there is another solution than this, then please let me know or else i will accept the slicesolution straight away.

我真的不喜欢它:x。如果还有其他解决方案,请告诉我,否则我将立即接受该slice解决方案。

采纳答案by Andy E

IMO, sliceis more appropriate than substringor replace. Try this:

IMO,slicesubstringor更合适replace。尝试这个:

jQuery("#imageID").attr(
    'src',
    jQuery("#imageBlock").css('background-image').slice(4,-1)
);

Here, you're slicing the string in between url(and ). See MDC on slicefor a detailed description of the method.

在这里,您在url(和之间切割字符串)。有关该方法的详细说明,请参阅切片上的MDC 。

回答by Darin Dimitrov

You need to extract the url part:

您需要提取 url 部分:

var backgroundImage = $("#imageBlock")
    .css('backgroundImage')
    .replace(/"/g,"")
    .replace(/url\(|\)$/ig, "");
jQuery("#imageID").attr('src', backgroundImage);

回答by davydepauw

It's because the url() string is wrapped around it. You'll need to strip it from the string, for example using the replace function...

这是因为 url() 字符串环绕在它周围。您需要将其从字符串中删除,例如使用替换功能...

var bgimg = jQuery("#imageBlock").css('background-image').replace('url(', '');
bgimg.replace(')', '');

回答by Alex Sexton

You would need to strip the url(and closing )parts out for that to work.

您需要剥离url(和关闭)部件才能工作。

So url(http://localhost:8080/images/1/myImage.png)

所以 url(http://localhost:8080/images/1/myImage.png)

becomes

变成

http://localhost:8080/images/1/myImage.png

http://localhost:8080/images/1/myImage.png

You could do this with a substring, a regex replace or any method of your choosing.

您可以使用子字符串、正则表达式替换或您选择的任何方法来执行此操作。

http://www.w3schools.com/jsref/jsref_replace.asp

http://www.w3schools.com/jsref/jsref_replace.asp

Perhaps:

也许:

jQuery("#imageID").attr('src',jQuery("#imageBlock").css('background-image').replace('url(','').replace(')','') )

jQuery("#imageID").attr('src',jQuery("#imageBlock").css('background-image').replace('url(','').replace(')','') )