在元素呈现之前做一些工作

时间:2014-04-14 23:49:14

标签: jquery

我想在渲染元素之前添加一些css样式(display: none)。

问题在于我尝试在jQuery插件中执行此操作,但我无法在标头中初始化插件。

我不确定在我的情况下有什么可能

插件:

(function ($) {
    var methods = {
        init: function (options) {
           if (options.collapsed_mode) {
              $('#menu').hide();
           }
        }
    };

    $.fn.dropright = function (method) {

        if (methods[method]) {
            methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' not found');
        }
        return true;
    };
});

页:

<?php include 'templates/header.php'; ?>
<div id="wrapper">
    <div id="menu">
       Hello, world ;)
    </div>
</div>
<?php include 'templates/fooer.php'; ?>

更新

我无法添加到css文件display: none,因为在不同的网页中,我需要display的不同值。例如,在包含插件的地方,我需要display: none,但对于任何其他插件,不应该隐藏该元素。

1 个答案:

答案 0 :(得分:2)

在元素呈现到页面上之前,您无法在元素上调用.hide()。 JavaScript不适合这里的工作。

相反,将display: none添加到元素的CSS中(并确保在元素之前加载CSS文件)。您已经注意到这应该只在某些页面上。凉。只在这些页面上加载此CSS。

#menu {
  display: none;
}
相关问题