javascript - 我如何知道跨度(div) block 中转运的​​内容的宽度和高度

标签 javascript jquery html css

有一个 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/

相关文章:

javascript - 如果所有元素(表格)的宽度与某些(200)像素相同,如何隐藏它们?

javascript - Jquery Ajax post 方法给出 null 值

JavaScript 日期 MAX_VALUE

javascript - Golang 网络套接字处理程序

javascript - angularjs/signalR Bootstrap 进度条不随 Controller 更改而更新

jquery 点击事件在 Firefox 中未触发

javascript - 禁用 swf 文件上的鼠标滚轮滚动?

html - 如何让我的背景图片在底部不被截断?

html - 如何将 div 添加到 Magento 页面

javascript - 模式仅在多个按钮上显示最后一个按钮内容