【发布时间】:2015-06-24 05:06:27
【问题描述】:
我有这个 HTML
<div class="picture"></div>
根据视口宽度,我想在 div 中添加小图像或大图像,使其看起来像这样:
<div class="picture"><img src="small.jpg></div>
这是我在网上某处找到的一些 jQuery,它似乎完全符合我的要求。我现在唯一被卡住的部分是 img src="....jpg" 事情。我怎样才能翻译这个成jQuery?
jQuery(document).ready(function() {
$(window).resize(function() {
var window_width = $(document).width();
if (window_width <= 1024) {
img src = "small.jpg"
} else {
img src = "large.jpg"
}
});
});
谢谢!
【问题讨论】:
-
<img>标签是否已经存在您只想更改源? -
试试 $('.picture').prepend('')
-
@Varun 不,img 标签不存在。只是空的 div
-
只需在下面查看我的答案,为了安全起见,我已经回答了这两种情况@Groen91
标签: javascript jquery html