【问题标题】:Get innerWidth of Bootstrap button (crossbrowser)获取引导按钮的内部宽度(跨浏览器)
【发布时间】:2015-09-01 06:10:01
【问题描述】:

我正在尝试获取引导按钮的大小(宽度),但我有一些奇怪的行为。

<div class="row">
    <div class="col-md-4 col-md-offset-4">
        <button type="button" class="btn btn-warning btn-sm">
             <span class="glyphicon glyphicon-floppy-disk" aria-hidden="true"></span> Save This Item
        </button>
    </div>
</div>

jquery

var siHeight = $('button').innerHeight();
var siWidth = $('button').innerWidth();

$('button').on('click', function() {

   var iHeight = $('button').innerHeight();
   var iWidth = $('button').innerWidth();

   alert("On click H x W" + iHeight + " x " + iWidth + " Before click H x W " + siHeight + " x " + siWidth);

});
  1. innerWidth 在按钮点击事件之前或之后不相等
  2. 使用的每种浏览器(IE10、Firefox、Chrome)的行为都不同

Bootply

我可以使用跨浏览器解决方案在单击事件之前和之后获得相同的大小吗? 谢谢

【问题讨论】:

    标签: jquery css twitter-bootstrap cross-browser


    【解决方案1】:

    您必须添加一些 CSS 以使按钮在单击时不会发生变化:

    .btn:focus,.btn:active {
         outline: none !important;
    }
    

    Bootply of edited code with css

    【讨论】:

    • 不幸的是,Firefox 和 chrome 的行为没有改变
    • 一定要使用innerWidth()和innerHeight()吗?如果你只使用 width() 和 height() 你会得到与添加 css 一致的结果。
    • 很遗憾,你说的不是真的。我使用了高度和宽度,但使用 chrome 或 firefox 的结果不同bootply.com/ljxHqnfrKO
    【解决方案2】:

    “前后不相等”的问题是因为css样式 :active, :focus , :hover 有轮廓。在这种情况下,您最好使用开发人员工具并在 :active、:focus、:hover 时检查您的 css 代码。

    另一方面,您可以简单地使用宽度而不是内部宽度。

    【讨论】:

    • 谢谢,但很遗憾我的第二个问题仍未得到解答
    猜你喜欢
    • 2012-05-18
    • 2011-01-21
    • 2013-02-06
    • 1970-01-01
    • 1970-01-01
    • 2015-01-18
    • 1970-01-01
    • 2014-04-23
    • 2010-10-01
    相关资源
    最近更新 更多