<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>
如果img的src属性为blank.gif,那么我想将src属性设置为我的图标url“/_layouts/images/myicon.gif”。我需要引用img作为这个$('。s4-titlelogo img')。
SO FAR:
iconurl = $('.s4-titlelogo a>img').attr("src");
if (iconurl == ""/_layouts/images/blank.gif")
{
}
答案 0 :(得分:15)
如果img的src属性为blank.gif ...
if($('.s4-titlelogo img').attr('src') === '/_layouts/images/blank.gif')
...然后我想将src属性设置为我的图标网址。
$('.s4-titlelogo img').attr('src', '/_layouts/images/myicon.gif');
编辑:立即三个downvotes?呃...为什么?
这是一个JSFiddle示例来证明这是有效的。
答案 1 :(得分:3)
这样做......
现场演示: http://jsfiddle.net/oscarj24/FW4kj/
$(function(){
//Get the element
elem = $('.s4-titlelogo img');
//If 'src' attribute contains 'blank.gif'
if (elem.prop('src').indexOf('blank.gif') > 0)
//Replace 'src' attribute with 'myicon.gif'
elem.prop('src', '/_layouts/images/myicon.gif');
//Alert new 'src' attribute just to verify
alert('New image url is: ' + elem.prop('src'));
});