繁体   English   中英

从javascript文件内的另一个javascript文件调用函数

[英]Calling a function from another javascript file inside a javascript file

我正在尝试从另一个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内部的函数Reply()。

Comment.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);
}

一旦您单击“ reply button”,就可以简单地调用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);
}

在newReply函数中,尝试更改replyBox.parentNode.remove(replyBox);

replyBox.parentNode.removeChild(replyBox);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM