[英]change img src using 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>
If the src attribute of the img is blank.gif, then i want to set the src attribute to my icon url "/_layouts/images/myicon.gif". 如果img的src属性为blank.gif,那么我想将src属性设置为我的图标网址“ /_layouts/images/myicon.gif”。 I need to reference the img as this $('.s4-titlelogo img').
我需要将img引用为$('。s4-titlelogo img')。
SO FAR: 至今:
iconurl = $('.s4-titlelogo a>img').attr("src");
if (iconurl == ""/_layouts/images/blank.gif")
{
}
If the src attribute of the img is blank.gif...
如果img的src属性为blank.gif ...
if($('.s4-titlelogo img').attr('src') === '/_layouts/images/blank.gif')
...then i want to set the src attribute to my icon url.
...然后我想将src属性设置为我的图标网址。
$('.s4-titlelogo img').attr('src', '/_layouts/images/myicon.gif');
Edit: Three downvotes instantly? 编辑:立即三票否决? Uh... why?
嗯...为什么?
Here's a JSFiddle example to prove that this works. 这是一个JSFiddle示例,以证明这是可行的 。
Just do this... 只是做这个...
Live demo: http://jsfiddle.net/oscarj24/FW4kj/ 现场演示: 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'));
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.