gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 01:07:06 25 4
gpt4 key购买 nike

在 Chrome 控制台中:

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

在渲染 HTML 时的代码中:

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

Cannot read property 'scrollWidth' of undefined

最佳答案

如果代码可以在浏览器控制台(又名开发工具)上运行,但不能与您的 JS 代码一起运行,则意味着(在大多数情况下)您的 JS 代码先运行DOM/HTML 实际上已加载。

您有 2 个选择:

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

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

相当于推荐的调用方式:

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

相关的有用问题,请参见:What does $(function() {} ); do?

关于javascript - 滚动宽度问题的最佳解决方案是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52346067/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com