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 關注
- 142 瀏覽
添加回答
舉報
0/150
提交
取消