jquery - 当宽度由 css 类设置时获取 div 的宽度

标签 jquery

这个 JS 脚本给了我一个警告框:

jQuery.UI.version = 1.0.2  ---   Box Width: undefined

如何选择#ressized

 <head>
    <title></title>
    <link href="StyleSheet.css" rel="stylesheet" type="text/css" />
    <script src="jquery-1.9.1.min.js" type="text/javascript"></script>
    <script src="jquery-ui-1.10.2.custom.min.js" type="text/javascript"></script>
       <style>
  #resizable { width: 200px; height: 150px; padding: 5px; border: 1px solid red;}

  </style>

      <script>
        $(function () {
            var boxWidth = $('#resizable').attr('width');
            alert("jQuery.UI.version = " + jQuery.ui.version + "  ---   Box Width: " + boxWidth);           
        });
  </script>


</head>
<body>
    <div id="resizable" class=">
        <div id="title">
            <h2>
            The expanding box
            </h2>
        </div>    
    </div>
</body>

最佳答案

请注意,您没有 width 属性:

<div id="resizable" class=">

这肯定会崩溃并设置为未定义var boxWidth = $('#resizing').attr('width');

有两种方法获取元素的宽度,例如:

通过.width()方式:

$('#resizable').width();

并通过.css()方式:

$('#resizable').css('width');

关于jquery - 当宽度由 css 类设置时获取 div 的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15945877/

相关文章:

jquery - 如何使用jquery每5秒添加顶部属性的值

object - 比较 jQuery 对象

javascript - 考虑将事件处理程序标记为 'passive' 以使页面响应更快

javascript - jQuery 自动完成 (1.8.5) - 未捕获的类型错误,属性 'source' 不是函数

jquery - 每当将元素添加到 DOM 时,将类添加到元素

javascript - 平铺的 jQuery 网格

jquery - jQuery:检查是否已嵌入嵌入式youtube视频-准备播放了吗?

Jquery - 单击父 div 时访问子值

javascript - 将对象转换为 ul li 元素

javascript - $(window).scrollTop() 在 safari 中不起作用