将javascript转换为jquery.width

将javascript转换为jquery.width,javascript,jquery,width,outerwidth,Javascript,Jquery,Width,Outerwidth,我使用这个脚本根据屏幕宽度加载大小图像 <script> var jt_width = screen.width; //var jt_height = screen.height; if (jt_width < 361) {document.write('<div class="small-image"><img src="http://www.example.com/small-image.jpg" alt="small image alt">

我使用这个脚本根据屏幕宽度加载大小图像

    <script>
var jt_width = screen.width;
//var jt_height = screen.height;
if (jt_width < 361) {document.write('<div class="small-image"><img src="http://www.example.com/small-image.jpg" alt="small image alt"></div>') } else { document.write('<div class="large-image"><img src="http://www.example.com/large-image.jpg" alt="large image alt"></div>')};
</script>

如何使用jquery获得相同的输出?

您的新脚本将无助于提高脚本的响应性。您需要添加一个侦听器:

<script>
var jt_width = screen.width;
function doAThing() {
  if (jt_width < 361) {
    document.write('<div class="small-image"><img src="http://www.example.com/small-image.jpg" alt="small image alt"></div>') } else { document.write('<div class="large-image"><img src="http://www.example.com/large-image.jpg" alt="large image alt"></div>')
  };
}

doAThing();
window.addEventListener('resize', doAThing);
</script>

var jt_width=屏幕宽度;
函数doAThing(){
如果(jt_宽度<361){
document.write(“”)else{document.write(“”)
};
}
doAThing();
addEventListener('resize',doAThing);
编辑:我很确定“调整大小”对定向效果很好,但如果您测试它,但效果不好,您也可以在函数上添加一个
orientationchange
侦听器:


window.addEventListener('orientationchange',doAThing)

这是一个使用jquery的实现:

$( window ).resize( function ( ) { 
     var width = $( window ).width( );
     var height=$( window ).height( );
     /**on resize you can change your content or code based on your condition*/
});
/*This event will be triggered when the window size will change */

要动态更改,在调整窗口大小时,它在浏览器中是否起作用?你有一个
onresize
事件监听器吗?今天晚些时候我会尝试这个,但我不理解这部分:“但是如果你发短信的话”。你是说“测试”吗?这个不行。请参见此处:调整大小时,其余内容将消失。在浏览器中,页面不断加载。什么不起作用?这似乎对我有用。现在我回来看看这个,还有几件事我也应该提一下。你真的不应该使用
文档。为此编写
。您应该使用另一种方法,如
element.appendChild()
。此外,您可能应该检查要附加的元素是否已经存在,也就是说,它不会每次都重新附加它。“什么不起作用?”请参见此处:jsfiddle.net/WendiT/vxykg8ae剩余内容在调整大小时消失。在浏览器中,页面会连续加载。我喜欢这个选项,但我不知道如何在第一次加载时获取图像,所以在调整大小之前。当我改变手机的方向时,这个能用吗?我也不能用这个。请看这里:图像根本没有加载,可能是我错误地实现了它。
$( window ).resize( function ( ) { 
     var width = $( window ).width( );
     var height=$( window ).height( );
     /**on resize you can change your content or code based on your condition*/
});
/*This event will be triggered when the window size will change */