function $(id) {return document.getElementById(id);}function hide(){$(id).style.display='none';}如何实现$("id").hide()方式使用<div id="box"></div>function show(){$(id).style.display='block';}function html(str){$(id).innerHTML=str;}如何实现类似jQuery的使用方式:使用$("box")返回document.getElementById(“box”)对象,使用$("box").show()显示元素,使用$("box").hide()隐藏元素,使用$("box").html("hello")设置内容
2 回答
鸿蒙传说
TA贡献1865条经验 获得超7个赞
<!DOCTYPE HTML><html><head><meta charset=UTF-8><title>recursion</title><style type="text/css"></style><script type="text/javascript"> 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 (); }</script></head><body> <div id="box"></div></body></html> |
千巷猫影
TA贡献1829条经验 获得超7个赞
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 回答
- 0 关注
- 179 浏览
添加回答
举报
0/150
提交
取消
