2010-12-20 31 views
2

我想從另一個JavaScript文件(在JavaScript文件中)基本上調用一個函數,但它似乎不工作。調用一個JavaScript文件中的另一個JavaScript文件的功能

在我的main.html中我已經宣佈這兩個文件,像這樣:

<script type="text/javascript" src="Jquery/jquery-1.4.4.js"> </script> 
<script type="text/javascript" src="src/Reply.js"> </script> 
<script type="text/javascript" src="src/Comment.js"> </script> 

和內部Comment.js我調用函數回覆()是內Reply.js ..

評論。 JS:

function Comment(message){ 
    var self = this; 
    var message = message; 
    // 
    var comment = document.createElement("li"); 
    comment.id = "comment"; 
    comment.style = "display: none;"; 
    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 
    var parent = document.getElementById("wall"); 
    parent.insertBefore(comment,parent.firstChild); 
    //effect after insertion 
    $("ul#wall li:first").fadeOut(); 
    $("ul#wall li:first").fadeIn(); 

    return comment; 
} 

function newReplyTxtBox(comment){ 
    var buttons = comment.getElementsByTagName("input"); 
    buttons.item(0).disabled="disabled"; 

    var replyBox = document.createElement("div"); 
    replyBox.id="replyBox"; 

    var replyTxt = document.createElement("input"); 
    replyTxt.id="replyTxt"; 
    replyTxt.type="text"; 
    replyTxt.value="Write a reply"; 
    replyTxt.onfocus = function(e){if(this.value==this.defaultValue) this.value='';}; 
    replyTxt.onblur= function(e){if(this.value=='') this.value=this.defaultValue;}; 
    replyBox.appendChild(replyTxt); 

    createButtons(replyBox); 

    comment.appendChild(replyBox); 
} 

function newReply(replyBox){ 
    var message = $("input#replyTxt").val(); 
    var reply = new Reply(message); 
    replyBox.parentNode.remove(replyBox); 
    var replyField = replyBox.parentNode.getElementsByTagName("ul").item(0); 
    replyField.appendChild(reply); 
} 

的newReply()的簡稱,一旦你點擊「回覆按鈕」,這是創造像這樣:

var submitBtn = button.cloneNode(); 
     submitBtn.value = "submit"; 
     submitBtn.addEventListener("click", function(){newReply(parent)},false); 
     parent.appendChild(submitBtn); 

Reply.js:

function Reply(replyMsg){ 
    var self = this; 

    var reply = document.createElement("li"); 
    reply.id="reply" 
    reply.textContent = replyMsg; 

    var deleteBtn = document.createElement("input"); 
    deleteBtn.value = "delete"; 
    deleteBtn.addEventListener("click", function(){deleteReply(reply)},false); 

    reply.appendChild(deleteBtn); 

    return reply; 
} 
function deleteReply(reply){ 
    reply.parentNode.removeChild(reply); 
} 
+1

你在控制檯中看到什麼樣的錯誤? – 2010-12-20 04:36:06

+0

reply.addEventListener(「click」,function(){newReplyTxtBox(parent)},false); 應該newReplyTxtBox不是簡單的newReply? – HBP 2010-12-20 05:02:29

+0

sry我貼了一個錯誤的..我剛剛更新了它 – twidizle 2010-12-20 05:25:55

回答

1

在newReply功能,嘗試改變replyBox.parentNode.remove(replyBox);

replyBox.parentNode.removeChild(replyBox);

相關問題