JavaScript更改img src属性而不使用jQuery [英] JavaScript change img src attribute without jQuery
本文介绍了JavaScript更改img src属性而不使用jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
需要帮助转换嵌套的 .attr
功能。
谢谢!
window.onresize = window.onload = function(){
if(window.innerWidth> 1536){
var logo = document.getElementById('rm');
logo.attr('src','img / rm2.png');
}
};
解决方案
你的意思是你想使用纯粹的javascript?
这应该这样做:
var logo = document.getElementById(' R M');
logo.src =img / rm2.png;
所以你的功能应如下所示:
window.onresize = window.onload = function(){
if(window.innerWidth> 1536){
var logo = document.getElementById('rm' );
logo.src =img / rm2.png;
}
};
注意:你也可以使用 element.setAttribute 。但是,请参阅此帖子了解更多信息:
何时使用setAttribute vs .attribute = in JavaScript?
the new iPad! Don't want to use jQuery because this is the only function needed on the page.
Need help converting the nested .attr
function.
Thanks!
window.onresize = window.onload = function () {
if (window.innerWidth > 1536) {
var logo = document.getElementById('rm');
logo.attr('src','img/rm2.png');
}
};
解决方案
You mean you want to use pure javascript?
This should do it:
var logo = document.getElementById('rm');
logo.src = "img/rm2.png";
So your function should look like :
window.onresize = window.onload = function () {
if (window.innerWidth > 1536) {
var logo = document.getElementById('rm');
logo.src = "img/rm2.png";
}
};
Note: You could also use element.setAttribute. BUT, see this post for more:
When to use setAttribute vs .attribute= in JavaScript?
这篇关于JavaScript更改img src属性而不使用jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文