首页 > jQuery 的attr( )方法为什么不能获取 <input type="button" />的宽高?

jQuery 的attr( )方法为什么不能获取 <input type="button" />的宽高?

</script>
        <style type="text/css">
            input{width:100px;height:70px;color: red;background-color: azure;}
        </style>
        <script type="text/javascript">
            $(function() {
                $("input").click(function() {
                    alert($("input").attr("width","height"));

                })

            })
        </script>

    </head>

    <body>
        <input type="button" value="显示"/>
        
    </body>


attr是attribute的缩写,而attribute的意思是属性


建议你看看jq手册的attr方法,看看它的介绍,看完你就明白了


$("input").width()
$("input").height()

$("input").css("width")
$("input").css("height")

你代码的问题有2,

  1. 宽高是css属性

  2. attr("width","height") 这是赋值的写法, 并不是同时获取width和height


你需要看看官方文档。

$(function() {
    $("input").click(function() {
        alert($(this).height() + 'px:' + $(this).width() + 'px');
    })
})

attr的用法:
http://www.css88.com/jqapi-1.9/attr/
height:
http://www.css88.com/jqapi-1.9/height/
prop:
http://www.css88.com/jqapi-1.9/prop/


attr.width()


谢谢邀请。使用width()/height()即可。attr()是取值或赋值用的。

var _w = $('input[type="button"]').width(), _h = $('input[type="button"]').height();
console.log('width:'+_w+' height:'+_h);
【热门文章】
【热门文章】