简体   繁体   English

正确的方法将html追加到Jquery / Javascript

[英]Correct way to append html to Jquery/Javascript

I have the following code: 我有以下代码:

<section class="section1">
    //Section one content
</section>
<section class="section2">
    <h4>Some Title</h4>

    <input type="hidden" class="keys" value="1"/>
    <div id="block-close-1"></div>
    <div class="block" id="block-1">
          //Block content
    </div>

</section>

<section class="section3">
    //Section3 content    
</section>

What I want to do is take some html and insert it after "block-1" 我想做的是拿一些html并在“block-1”之后插入它

The HTML I wish to add looks like this: 我想添加的HTML如下所示:

    <input type="hidden" class="keys" value="2"/>
    <div id="block-close-2"></div>
    <div class="block" id="block-2">
          //Block content
    </div>

I have tried this: 我试过这个:

var html = '//code as above';
$( "html" ).insertAfter( "#block-1");

The error I get is this: 我得到的错误是这样的:

Uncaught HierarchyRequestError: Failed to execute 'insertBefore' on 'Node': The new child element contains the parent. Uncaught HierarchyRequestError:无法在'Node'上执行'insertBefore':新的子元素包含父元素。

I have also tried this: document.getElementById('#block-4').appendChild(html); 我也试过这个:document.getElementById('#block-4')。appendChild(html);

Error: 错误:

Uncaught TypeError: Cannot read property 'appendChild' of null 未捕获的TypeError:无法读取null的属性'appendChild'

What is the correct way to add new HTML to the existing HTML? 将新HTML添加到现有HTML的正确方法是什么?

You want $(html) not $("html") 你想要$(html)而不是$("html")

The first tries to add the contents of the variable html, the second tries to find the tag html on the DOM and add it~ 第一个尝试添加变量html的内容,第二个尝试在DOM上找到标记html并添加它〜

The native JavaScript approach would be this: 本机JavaScript方法是这样的:

document.getElementById("block-1").insertAdjacentHTML("afterend",html);

jQuery approach: jQuery方法:

$("#block-1").after(html);

From jQuery documentation for insertAfter ... insertAfter的 jQuery文档...

The .after() and .insertAfter() methods perform the same task. .after()和.insertAfter()方法执行相同的任务。 The major difference is in the syntax-specifically, in the placement of the content and target. 主要区别在于语法特定,内容和目标的放置。 With .after(), the selector expression preceding the method is the container after which the content is inserted. 使用.after(),方法之前的选择器表达式是插入内容之后的容器。 With .insertAfter(), on the other hand, the content precedes the method, either as a selector expression or as markup created on the fly, and it is inserted after the target container. 另一方面,使用.insertAfter(),内容在方法之前,作为选择器表达式或动态创建的标记,并在目标容器之后插入。

That is, if you want to place your new html after block-1 you select block-1 and call insertAfter() on it, passing the new html as the parameter - 也就是说,如果你想在block-1之后放置你的新html,你选择block-1并在其上调用insertAfter(),传递新的html作为参数 -

$('#block-1').insertAfter( $(html) );

Or you could keep the order you have if it's more intuitive to you, but use .after() 或者你可以保留你的订单,如果它对你更直观,但是使用.after()

$(html).after($('#block-1'));

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

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