0

在这段代码中:

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 的祖父母之后插入一段代码。有什么想法吗?:)

4

1 回答 1

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

于 2013-03-11T15:27:45.737 回答