나는 파라미터연속 JS 방법
function getElement() {
var scope = document;
this.by = function(data) {
for (key in data) {
if (key == 'id') scope = scope.getElementById(data.id);
if (key == 'tag') scope = scope.getElementsByTagName(data.tag);
}
return scope;
}
}
function getEl(data) { return new getElement().by(data); }
이 getEl(id : 'divId', tag : 'span')
같은 호출과는 DIV 'divId는'모든 지속 기간을 선택할 것이다에 기초하여 어떤 요소가 선택하는 JS 기능을 갖는다.
이제는 이전에 선택한 모든 범위에서 CSS를 변경하는 style이라는 다른 함수 (내부 function 요소)를 만들고 싶습니다.
뭔가
function getElement() {
var scope = document;
this.by = function(data) {
for (key in data) {
if (key == 'id') scope = scope.getElementById(data.id);
if (key == 'tag') scope = scope.getElementsByTagName(data.tag);
}
return scope;
}
this.style = function(data) {
console.log(data);
}
}
처럼 나는
getEl({id : 'divId', tag : 'span').style({display : 'none'})
처럼 뭔가를 할 수 있도록하고 싶습니다하지만이 작동하지 않습니다와 나는 getEl({id: "divId", tag: "span"}).style is undefined
오류가 발생합니다.
ps : 이것은 학습 목적으로 만 사용되며 jQuery 또는 다른 프레임 워크를 제안하지 마십시오. :)
친절에 감사드립니다!
"연속적인 js 메서드"는 체인 –
:) 감사합니다! 당신이 아마 JS가 나에게 꽤 새로운 것을 알았던 것에 따라. –
그건 그렇고,'getElementById'는'Document' 객체 만 지원합니다. 'Element' 또는'Node'에서 작동하지 않을 것입니다. – cem