本文介绍了普通的javascript到jquery - clientHeight的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下脚本

(function(win){
    var doc = win.document;
    if (doc.querySelector && doc.addEventListener) {
        var toggler = doc.querySelector('.toggle-menu')
        var menu = doc.querySelector('.main-nav ul');
        menu.style.height = '0px';
        toggler.addEventListener('click',function(e) {
            e.preventDefault();
            if (menu.style.height == '0px') {
                menu.style.height = 'auto';
                if (menu.clientHeight != 0) {
                    menu.style.height = menu.clientHeight+'px';
                }
            } else {
                menu.style.height = '0px';
            }
        });
    }
})(this);

该脚本的jQuery版本是什么,因为我找不到与clientHeight等效的jQuery 。

What will be the jQuery version of that script, since i can't find a jQuery equivalent to clientHeight.

推荐答案

clientHeight 不是jQuery属性。它是在Internet Explorer中引入的,但不是W3C规范的一部分。它看起来只在Firefox和Internet Explorer中受支持。我刚刚测试过它可以在最新版本的Chrome中运行。不确定结果是否是跨浏览器的标准,但我在下面发布的链接表明没有。

clientHeight is not a jQuery property. It was introduced in Internet Explorer, but isn't part of the W3C specifications. It looks like it is only supported in Firefox and Internet Explorer. I've just tested that it works in the latest version of Chrome, though. Not sure if results are standard across browsers, though the link I posted below suggests no.

此外,Mozilla建议使用以下公式来替代浏览器支持它:

Also, Mozilla suggests the following formula to be used in place for browsers that don't support it:

我假设这是元素本身的滚动条,而不是整个浏览器窗口,除非元素占用整个窗口。

I'm assuming that is the scrollbar of the element itself, not the entire browser window, unless the element takes up the entire window.

来源:




  • clientHeight/clientWidth returning different values on different browsers
  • https://developer.mozilla.org/en/DOM/element.clientHeight

这篇关于普通的javascript到jquery - clientHeight的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 23:21