да не) Понял как делать:
<div id='div'>1</div>
<div>2</div>
<script>
function $() {
return {
id: function (id) {
this.elem = document.getElementById(id);
this.id = id;
return this;
},
add: function (add) {
this.elem.innerHTML += add
return this;
},
html: function () {
return this.elem.innerHTML
return this;
},
tag: function (tag, index) {
this.elem = document.getElementsByTagName(tag)[index]
return this;
},
write: function (write) {
this.elem.innerHTML = write
return this;
},
src: function () {
return this.elem.src
return this;
}
};
}
alert($().tag('div', 0).html()) /* работает, хотя раньше фаерьаг писал: html() - is not a fuction */
alert($().id('div').html()) // также работает
/* Дело в том, что раньше все ф-ии получали доступ к элементу через docu...ById(this.id)(и если мы брали элемент по тэгу, то ничего не работало), сейчас же весь элемент "храниться" в this.elem */
</script>
|
если я правильно понимаю, строки выделенные цветом, выполняться не будут
<div id='div'>1</div> <div>2</div> <script> function $() { return { id: function (id) { this.elem = document.getElementById(id); this.id = id; return this; }, add: function (add) { this.elem.innerHTML += add return this; }, html: function () { return this.elem.innerHTML return this; }, tag: function (tag, index) { this.elem = document.getElementsByTagName(tag)[index] return this; }, write: function (write) { this.elem.innerHTML = write return this; }, src: function () { return this.elem.src return this; } }; } </script> Также не совсем ясно, зачем оно надо и надо ли. Куда подевались точки с запятой? Вы читаете, что Вам пишут? |
wtf? Что с форумом, отправил ответ уже давно, а он не дошел...
так вот, спасибо про return, просто когда смотрел топик про подобную схему:
function a() {
return {
a: function () { return this; }
b: function () {return this;}
}
}
Вот и ставил везде rerurn this :agree: А зачем точка с запятой? Ради оформления? Ладно, впредь буду ставить |
| Часовой пояс GMT +3, время: 18:57. |