开发者

hiding an element with javascript, in a modular way

I'm trying to create some functionality akin to .hide in jquery, but just using plain javascript, here's what I have so far:

(function() {

addLoadEventHandler(function() {


        get开发者_JS百科ByName = (function(name){ 
            return document.getElementsByTagName(name)
        })

        hide = (function(){
            return style.display = 'none'

        })

    });

return {

    getByName: getByName,
    hide: hide
};
}());

so terribly incomplete, but am I heading along the right track here. Sorry, bit of a noob.


You need context for the hide function. Either you wrap a DOMElement like jQuery does, or you need to pass it in. I'll give examples for both:

var incomplete = (function () {

    function hide(DOMElement) {
        DOMElement.style.display = 'none';
    }

    return {
        hide: hide
    };
}());

// use like:
incomplete.hide(document.getElementById("container"));

or

var incomplete = function (context_element) {
    function hide() {
        context_element.style.display = 'none';
    }

    return {
        hide: hide
    };
};

// use like:
incomplete(document.getElementById("container")).hide();    // like jQuery


Sort of, you need to assign it to something

mymodule = function () {

        //"private" variables:
        var privateVar = "only from within this module."

        //"private" methods:
       var getByName = function(name){ 
           return document.getElementsByTagName(name)
       }

       hide = function(){
           return style.display = 'none'
       }

       // public interface
        return  {
           getByName: getByName,
           hide: hide           
                }
        };

}(); // the parens here cause the anonymous function to execute and return
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜