有一个 block :
<span id="xCUBE"></span>
哪个被定时器重载了:
<script type="text/javascript">
function show() {
$.ajax({
url: "/table/" + Math.floor(Math.random() * ( 999 )) + "/",
cache: false,
success: function(html){
$("#xCUBE").html(html);
}
});
}
$(document).ready(function(){
show();
setInterval('show()',500);
});
</script>
在这个 block id="xCUBE"建表,需要检查它是否放在当前窗口中。也就是找到窗口的实际宽度和 block 的宽度,比较,如果 table 比屏幕宽,就要求更短或更窄的 table 。如果你问盒子的宽度和额头上的一个窗口,我得到的宽度和高度都是0:
<script type="text/javascript">
function doResize( ) {
$('body').append('<div>Win WxH= ' + $(window).width()
+ 'x' + $(window).height() + ' (table WxH = '
+ $("#xCUBE").width() + 'x' + $("#xCUBE").height()
+')</div>');
}
$(function() {
doResize();
$(window).resize(doResize);
});
</script>
脚本在 id="xCUBE"中看不到表的动态内容。如何获取可重载内容的宽高?
最佳答案
您不应该在添加新内容后调整大小吗?
function show() {
$.ajax({
url: "/table/" + Math.floor(Math.random() * ( 999 )) + "/",
cache: false,
success: function(html){
//do resize and pass the content
doResize(html);
}
});
}
function doResize(html) {
//append to target and measure while its not visible (but still has sizes)
$("#xCUBE").html('<div style="visibility:hidden">'+html+'</div>');
//remember them
cubeWidth= $("#xCUBE").width();
cubeheight=$("#xCUBE").heigth();
//empty that thing
$("#xCUBE").empty()
//append your WxH=? stuff, i hae no idea
$('body').append('<div>Win WxH= ' + $(window).width()
+ 'x' + $(window).height() + ' (table WxH = '
+ cubeWidth + 'x' + cubeheight
+')</div>');
//and finally append the visible content
$("#xCUBE").html(html);
}
$(document).ready(function(){
//call show initially
show();
var k= setInterval('show()',500);
$(window).resize(function(){
//clear and reset interval and show imediatly, this could create thousands of ajax requests maybe some throttle, google it
clearInterval(k);
show();
k=setInterval('show()',500);
});
});
});
已更新
关于javascript - 我如何知道跨度(div) block 中转运的内容的宽度和高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22488228/