insertBefore语句中的参数无效

时间:2013-03-11 15:25:02

标签: javascript

在此代码中:

function comrespond(){
    function addresform(){
    var resid = this.getAttribute('id'),
    grandParent = this.parentNode.parentNode,
    newrespondform = '<div class="commentresponse"><span></span><span><p class="author">Leave a reply:</p><form id="commentform" action="http://split.snippetspace.com/wp-comments-post.php" method="post" onsubmit="addcomment(); return false;"><input id="author" aria-required="true" name="author" type="text" placeholder="Your name"/><input id="email" aria-required="true" name="email" type="text" placeholder="Email address" /><textarea id="comment" aria-required="true" name="comment" rows="8"></textarea><input id="submit" name="submit" type="submit" value="Post Comment" /><input id="comment_post_ID" name="comment_post_ID" type="hidden" value="'+resid+'" /></form></span></div>';
    grandParent.parentNode.insertBefore(newrespondform, grandParent.nextSibling);
    }
    var responsebtn = document.getElementsByClassName('comment-reply-link');
    for(var i = 0; i < responsebtn.length; i++){
                    bindEvt(responsebtn[i], "click", addresform);
                };

此行在IE10中返回为“无效参数”:

grandParent.parentNode.insertBefore(newrespondform, grandParent.nextSibling);

我似乎无法想出这个。它应该在cetain div的祖父母之后插入一段代码。有什么想法吗? :)

1 个答案:

答案 0 :(得分:2)

newrespondform是一个字符串。 insertBefore采用DOM元素。你应该把它改成:

newrespondform = document.createElement('div');
newrespondform.innerHTML = '<span></span><span><p class="author">Leave a reply:</p><form id="commentform" action="http://split.snippetspace.com/wp-comments-post.php" method="post" onsubmit="addcomment(); return false;"><input id="author" aria-required="true" name="author" type="text" placeholder="Your name"/><input id="email" aria-required="true" name="email" type="text" placeholder="Email address" /><textarea id="comment" aria-required="true" name="comment" rows="8"></textarea><input id="submit" name="submit" type="submit" value="Post Comment" /><input id="comment_post_ID" name="comment_post_ID" type="hidden" value="'+resid+'" /></form></span>';

以下是insertBefore:https://developer.mozilla.org/en-US/docs/DOM/Node.insertBefore

的文档