이 질문에 대한 힌트를 어디서도 찾을 수 없기 때문에 완전히 잘못된 나무를 짖는 것일 수 있습니다.
나는 생성자 함수를 넣는 자바 스크립트 네임 스페이스를 만들고 있습니다.
var SEP=SEP||{};
SEP.person=function(name)
{
this.name=name
this.sayName=sayName
function sayName()
{
return this.name;
$(document).ready
(
function()
{
$('body').css('background', 'red');
}
);
}
}
그런 다음 HTML에서 함수를 호출하고 있습니다.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>test</title>
<meta name="generator" content="BBEdit 10.5" />
</head>
<body>
<script src="libraries/jquery/javascript/jquery-1.11.0-min.js"></script>
<script src="libraries/sep/javascript/sep.js"></script>
<p>Hello</p>
<script>
var bob=new SEP.person("bob");
word=bob.sayName();
document.write(word);
</script>
</body>
</html>
첫 번째 질문은 왜 이것이 작동하지 않습니까? 두 번째 질문은 어떻게 작동시킬 수 있습니까? 세 번째 질문은 JQuery를 사용하여 좀 더 복잡한 생성자를 만들고 싶다면 문서 준비 작업을 반복해야합니까, 아니면 더 나은 방법이 있습니까?
미리 감사드립니다.
실제로 생성자가 잘 작동합니다. 그러나 document.write
코드에서 사용해서는 안됩니다 . 대신 .append()
.html()
또는 .text()
메소드를 사용하여 html 태그에 값을 추가 할 수 있습니다 . 또한 sayName()
방법에 약간의 변경이 있어야합니다 return
. Css는 . 그렇지 않으면 해당 코드 줄에 맞지 않습니다.
var SEP = SEP || {};
SEP.person = function (name) {
this.name = name
this.sayName = sayName
function sayName() {
$('body').css('background', 'red');
return this.name;
}
}
var bob = new SEP.person("bob");
word = bob.sayName();
$("span").text(word);
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다