What's The Correct Way To Set Src Attribute In JQuery?

前端 未结 5 737
日久生厌
日久生厌 2020-12-15 16:07

Suppose I have the following HTML:


I would like to switch the src to bar2.jpg

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-15 16:35

    The first wey is just fine, no reason to remove it first.

    $("#foo").attr("src", "bar2.jpg");
    

    $.attr serves both to get the existing attribute and to change it (depending on whether theres one or two arguments). Your situation is exactly what he second functionality is intended for, and the attribute 'src' is not special.

    http://api.jquery.com/attr/

提交回复
热议问题