繁体   English   中英

滚动宽度问题的最佳解决方案是什么?

[英]What is the best solution for scrollWidth problem?

在Chrome控制台中:

$('.table-responsive').width(); //working
$('.table-responsive')[0].scrollWidth; //working

在呈现HTML的代码中:

$('.table-responsive').width(); //working
$('.table-responsive')[0].scrollWidth; //not-working

无法读取未定义的属性“ scrollWidth”

如果代码可以在浏览器的控制台( 也称为dev tools )上运行并且不能与您的JS代码一起使用,则这( 在大多数情况下 )意味着您的JS代码在实际加载DOM / HTML之前运行。

您有2个选择:

  1. 将您的script标签放在页面底部( </body>之前
  2. 使用jQuery的.ready() ,例如:

$(document).ready(function(){ ... });

这等效于建议的调用方式:

$(function() { ... });

相关的有用问题,在SO上: $(function(){})是什么? 做?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM