如何实现$("id").hide()函数

2025-02-26 06:16:11
推荐回答(2个)
回答1:


function $(id) { 
  
     this.hide = function(){
         document.getElementById(id).style.display='none';
     };
     
     this.show = function() {
         document.getElementById(id).style.display='block';
     };
     
     this.html = function(str){         
         document.getElementById(id).innerHTML=str;
     }
}

要实现的功能,就要使用面向对象编程,上面的例子很简单,只是实现你问题中最简单的三个方法,让你了解下思想


如果要实现Jquery的大致功能,结构会较复杂,估计一下子吸收不了,慢慢来吧

回答2:





recursion



function $ (id)
    {
    return document.getElementById (id);
    };
    $.prototype = Element.prototype;
    $.prototype.constructor = $;
    $.prototype.hide = function ()
    {
    this.style.display = 'none';
    return this;
    };
    $.prototype.show = function ()
    {
    this.style.display = 'block';
    return this;
    };
    $.prototype.html = function (str)
    {
    this.innerHTML = str;
    return this;
    };
    onload = function ()
    {
    $ ('box').html ("spider man").hide ().show ();
    }





相关问答