使用jquery更改img src

Ath*_*ali -1 html javascript css jquery

<div class="s4-titlelogo">
 <a href="/sites/mysite">
<img name="onetidHeadbnnr0" id="ctl00_onetidHeadbnnr2" style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px;" alt="bobpub" src="/_layouts/images/blank.gif" complete="complete"/>
</a>
</div>
Run Code Online (Sandbox Code Playgroud)

如果img的src属性为blank.gif,那么我想将src属性设置为我的图标url"/_layouts/images/myicon.gif".我需要引用img作为这个$('.s4-titlelogo img').

至今:

iconurl = $('.s4-titlelogo a>img').attr("src");
            if (iconurl == ""/_layouts/images/blank.gif")
            {

            }
Run Code Online (Sandbox Code Playgroud)

Jam*_*lly 15

如果img的src属性是blank.gif ...

if($('.s4-titlelogo img').attr('src') === '/_layouts/images/blank.gif')
Run Code Online (Sandbox Code Playgroud)

...然后我想将src属性设置为我的图标网址.

$('.s4-titlelogo img').attr('src', '/_layouts/images/myicon.gif');
Run Code Online (Sandbox Code Playgroud)

编辑:立即三个downvotes?呃...为什么?

这是一个JSFiddle示例来证明这是有效的.