........... ........... ........... $("#smth").src = "....."...">

JQuery src add weird class to IMG

I noticed that with:

<img id="smth" src="#" />
...........
...........
...........
$("#smth").src = ".....";

and a weird class is added to the element img:

enter image description here

(this does not happen if I do not perform this).
Any thoughts?

+4
source share
1 answer
$("#element")

returns a jquery object representing the elements matching the selector. In this case, it represents one element. When do you intend to install it srcas follows

$("#element").src = ".....";

you are actually trying to set the attribute of srcthis jquery object. Instead, there are several possible solutions. Examples:

atr

$("#element").attr("src", ".....");

prop

$("#element").prop("src", ".....");

array element

$("#element")[0].src = ".....";

receive

$("#element").get(0).src = ".....";
+3
source

Source: https://habr.com/ru/post/1652318/


All Articles