2010-12-20 55 views
1

我已經在我的javascript中定義了幾個函數,這些函數完美地工作,但是當我將它放入原型中時,它似乎不起作用。在javascript中使用原型

wall.html:

<script type="text/javascript" src="Jquery/jquery-1.4.4.js"> </script> 
<script type="text/javascript" src="src/CommentManager.js"> </script> 
<script type="text/javascript" src="src/Reply.js"> </script> 
<script type="text/javascript" src="src/Comment.js"> </script> 
<script type="text/javascript"> 
    $(document).ready(function(){ 
     CommentManager(); 
     $("form#newMessage").submit(function(){ 
      var message = $("input#newMessageTxt").val(); 
      var newComment = new Comment(message); 
     }); 
    }); 
</script> 
</head> 

<body> 
    <div class="message"> 
     <form id="newMessage"&gt;> 
      <input type="text" id="newMessageTxt" height="200px" value="Write a message" onfocus="if(this.value==this.defaultValue) this.value='';" onblur="if(this.value=='') this.value=this.defaultValue;" /> 
      <input type="submit" value="Submit" ></button> 
     </form> 
    </div> 
</body> 

,但怪異的一部分是,當我運行googlechrome調試工具,在$( 「#形式NewMessage作爲」)提交完全不來電。所以評論(消息),從未創建(這是我已經設置了原型功能)

Comment.js:

function Comment(message){ 
    var self = this; 
    var message = message; 

    var comment = document.createElement("li"); 
    comment.id = "comment"; 
    comment.textContent = message; 
    //empty reply field 
    var replyField = document.createElement("ul"); 
    replyField.id = "replyField"; 
    //create the appropriate buttons 
    createButtons(comment); 
    //append the replyField 
    comment.appendChild(replyField); 
    //insert into wall 
    addComment(comment); 
    //effect after insertion 
    Effect(comment); 
    $(comment).mouseleave(function() {mouseOut(comment);}); 
    $(comment).mouseenter(function() {mouseOver(comment);}); 
    return comment; 
} 
Comment.prototype={ 
    deleteComment : function (comment){ 
     $(comment).fadeOut(); 
     setTimeout(function() {comment.parentNode.removeChild(comment);},500); 
    }, 
//there are more methods here 
} 

Commentmanager.js:

function CommentManager(){ 
    var owner = null; 

    var wall = document.createElement("ul"); 
    wall.id = "wall"; 

    document.body.appendChild(wall); 
    return wall; 
} 

function addComment(comment){ 
    var wall = document.getElementById("wall"); 
    wall.appendChild(comment); 
} 
+0

不幸的是,我所能做的只是重新編碼,因爲我不會說jquery。 '原型'有點誤導。 – 2010-12-20 20:18:02

+0

你試過調試嗎? – SLaks 2010-12-20 20:22:35

+0

實際上,'原型'是javascript中的一個描述性術語,與軟件開發中的其他任何地方有不同的含義(不幸:():請參閱http://javascript.crockford.com/prototypal.html來描述原型'在js中意味着'也許'javascript-prototype'應該是一個標籤? – 2010-12-20 20:29:41

回答

0

哪裏createButtons定義? (在Comment.js)?

此外,您在題爲去年文件Commentmanager.js,但wall.html有CommentManager.js(請注意wall.js首都M)。我假設這是SO問題的一個錯誤,但要確保服務器上的文件名與html腳本標記匹配。