简体   繁体   English

通过在元素后附加html来导致jQuery2错误

[英]jQuery2 error by appending html after an element

I tried to append HTML after an element with this simple lines: HTML to copy: 我试图用一个简单的行后面的元素追加HTML:要复制的HTML:

<div class="commentLayout">
    <form method="post" action="...">
        <textarea cols="10" rows="10" name="taCommentContent">
        </textarea>
        <input type="hidden" name="newsId" value="">
        <button type="submit" class="btn btn-success">speichern</button>
    </form>
</div>

And this is the JavaScript part that appends it: 这是附加它的JavaScript部分:

function createComment (id)
{
    var $comment = $(".commentLayout").first().clone();
    $comment.removeClass("commentLayout").addClass("commentEdit");
    $comment.find("input[name=newsId]").first().val(id);
    $(this).after($comment);
}

In browser console (with source map) it shows me the following error: 在浏览器控制台(带有源映射)中,它显示以下错误:

jquery2x.min.js:3 Uncaught TypeError: Cannot read property 'createDocumentFragment' of undefined
buildFragment @ jquery2x.min.js:3
domManip @ jquery2x.min.js:3
after @ jquery2x.min.js:
createComment @ frontend.js:716(anonymous function) @ VM219:1

I know what undefined means, but how came this error? 我知道undefined意味着什么,但是怎么会出现这个错误? I do not understand what went wrong here. 我不明白这里出了什么问题。 To clearify my question, I call this function from an link like this: 为了澄清我的问题,我从这样的链接调用此函数:

<!-- variable numbers comes from a loop -->
<a href="javascript:createComment(1);">comment this</a>

The problem is in this line: 问题出在这一行:

$(this).after($comment);

The value of "this" is by default the window object. “this”的值默认为window对象。 In the following snippet you may see a way to call your function and the value of "this" keyword. 在下面的代码段中,您可能会看到一种调用函数的方法以及“this”关键字的值。

In jQuery there is a possibility to change the context of a function: 在jQuery中,有可能更改函数的上下文:

$.proxy(createComment, this)(1);

and in javaScript you can use: 在javaScript中你可以使用:

createComment.call(this, 1);

or 要么

createComment.apply(this, [1]);

An alternative way is to pass as argument the "this" context variable to the function like in the snippet. 另一种方法是将“this”上下文变量作为参数传递给函数,就像在代码片段中一样。

The snippet: 片段:

 function createComment (obj, id) { var $comment = $(".commentLayout").first().clone(); $comment.removeClass("commentLayout").addClass("commentEdit"); $comment.find("input[name='newsId']").first().val(id); $(obj).after($comment); console.log('this is: ' + this + ' while obj is: ' + obj); } $(document).on('click', 'button[type="submit"]', function(e) { e.preventDefault(); createComment(this, 1); }); 
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> <script src="https://code.jquery.com/jquery-1.12.1.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> <div class="commentLayout"> <form method="post" action="..."> <textarea cols="10" rows="10" name="taCommentContent"> </textarea> <input type="hidden" name="newsId" value=""> <button type="submit" class="btn btn-success">speichern</button> </form> </div> 

UPDATE UPDATE

You do not have any undefined value. 您没有任何未定义的值。 Because the variable this is the window object you cannot insert after (jQuery) an object. 因为变量this是窗口对象,所以无法 (jQuery)对象之后插入。 You need a valid DOM object in order to apply the jQuery.after().... 您需要一个有效的DOM对象才能应用jQuery.after()....

New snippet using the anchor: 使用锚点的新代码段:

 function createComment (obj, id) { var $comment = $(".commentLayout").first().clone(); $comment.removeClass("commentLayout").addClass("commentEdit"); $comment.find("input[name='newsId']").first().val(id); $(obj).after($comment); console.log('this is: ' + this + ' while obj is: ' + obj); return false; } 
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> <script src="https://code.jquery.com/jquery-1.12.1.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> <!-- variable numbers comes from a loop --> <a href="javascript:createComment(document.getElementsByClassName('commentLayout')[0], 1);">comment this</a> <div class="commentLayout"> <form method="post" action="..."> <textarea cols="10" rows="10" name="taCommentContent"> </textarea> <input type="hidden" name="newsId" value=""> <button type="submit" class="btn btn-success">speichern</button> </form> </div> 

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

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