javascript - How to set the image src using jQuery -
I'm trying to change the image src attribute using jQuery
jQuery ("# ImageID") .attr ('src', 'http: // localhost: 8080 / picture / 1 / myImage.png');
I can change the src attribute using the code above, but when I try: -
jQuery ("# imageID "). Attr ('Src', jQuery ("# imageBlock"). CSS ('background-image'));
I am unable to change src
provided
warning (jQuery ("#Imageblock"). CSS ( 'background image'));
Returns:
url ()
Edit # 1 just when I had to accept the solution I should say, almost all the solutions work in FF. I tried:
- piece (4, -1);
- Split ("(") [1]> then (")", "");
I think others will also work. But none of the solutions is working in IE. The reason: FF Returns:
url ()
IE returns:
url ("")
^ ^ See quotation marks here
Now, what is the usual way of setting up source ARTR I need to test the browser if it is IE Yes or No?
This is the job code.
var src = ""; If (jQuery.browser.msie) {src = jQuery ("# imageBlock"). CSS ('background-image'). Piece (5, -2); } Else {src = jQuery ("# imageBlock"). CSS ('background-image'). Piece (4, -1); } JQuery ("# imageID"). Attr ('src', src);
I do not really like: x If there is another solution besides this, please let me know or else I will accept the piece
solution directly.
imo, piece
substring
or Is more suitable than replace
. Try it out:
jQuery ("# imageID"). Attr ('src', jQuery ("# imageBlock"). CSS ('background-image') piece (4, -1)));
Here, you are changing the string between url (
and )
. See on MDC for a detailed description of the method.
Comments
Post a Comment