简体   繁体   English

将HTML插入目标为在加载DOM之后添加的DIV ID的页面中?

[英]Insert HTML into page targeting a DIV ID that is added after the DOM has loaded?

I have a page that has a Modal that generates HTML from an AJAX request when each Modal is clicked on. 我有一个页面,其中包含一个模态,当单击每个模态时,该模态会根据AJAX请求生成HTML。

Inside the modal comments can be posted and in it;s AJAX response should insert new comment into the modal HTML with this... 可以在模态注释内部发布和添加模态注释; AJAX响应应在此将新注释插入模态HTML中...

$(result.html).hide().insertBefore('#addCommentContainer').slideDown();

For some reason it does not insert the HTML on success of new comment AJAX response. 由于某种原因,它不会在新注释AJAX响应成功时插入HTML。

I think it might because #addCommentContainer is added after the DOM is loaded. 我认为可能是因为#addCommentContainer是在加载DOM之后添加的。

So with that known how can I insert HTML after the DIV ID #addCommentContainer when #addCommentContainer is added after DOM load? 因此,与已知的我怎么能在DIV ID后插入HTML #addCommentContainer#addCommentContainer DOM加载后加入?


UPDATE TO EXPLAIN BETTER... 更新以更好地说明...

1) page is loaded and DOM is loaded 1)页面已加载且DOM已加载

2) AJAX request is made which adds a DIV with ID #addCommentContainer to page. 2)发出AJAX请求,将ID为#addCommentContainer的DIV添加到页面。
<div id="addCommentContainer">this div was added to page after the dom was loaded from an AJAX request</div>

3) Now another separate AJAX request is made which is trying to add comments to the HTML DOM. 3)现在,提出了另一个单独的AJAX请求,试图将注释添加到HTML DOM。 AJAX reuquest POST comment to server and server returns success and tries to append comment HTML to the previous DIV with ID #addCommentContainer that was added to the DOM before this AJAX request. 对服务器的AJAX reuquest POST注释和服务器返回成功,并尝试将注释HTML附加到ID为#addCommentContainer的先前DIV中,该ID已在此AJAX请求之前添加到DOM。

It does so with this jQuery code where result.html holds the comment HTML that should be added to the DOM: 这样做是通过以下jQuery代码实现的,其中result.html包含应添加到DOM的注释HTML:
$(result.html).hide().insertBefore('#addCommentContainer').slideDown();

The problem This HTML for the new comment is NOT added to the DOM. 问题新注释的此HTML未添加到DOM。 I assume it is because jQuery cannot find #addCommentContainer since it was added after the DOM was loaded. 我认为这是因为jQuery找不到#addCommentContainer因为它是在加载DOM之后添加的。

Generally when I need to use an elelment that was added after DOM load I can use document like this.... 通常,当我需要使用在DOM加载后添加的元素时,可以使用这样的document ...。
$(document).on('click', '#addCommentContainer', function(e){

However in this case it is not a simple click event and instead it needs to find the element so it can append more HTML to it. 但是,在这种情况下,这不是简单的click事件,而是需要找到该元素,以便可以向其添加更多HTML。

You could place async: false in the options of your first AJAX request. 您可以在第一个AJAX请求的选项中放置async:false。 It seems to me it's quite possible that your second request returns sooner then your first one. 在我看来,您的第二个请求比第一个请求早返回的可能性很大。 AJAX request are asynchronous unless specified otherwise. 除非另有说明,否则A​​JAX请求是异步的。 A more common solution would be to place the second request in the callback of the first request. 一个更常见的解决方案是将第二个请求放在第一个请求的回调中。

Can you not use the ready() or load() functions? 您不能使用ready()或load()函数吗? I believe How can I call a function after an element has been created in jquery? 我相信在jquery中创建元素后如何调用函数? may contain the answer to what you are trying to achieve. 可能包含您要实现的目标的答案。

UPDATE 更新

Try wrapping your jQuery in $(document).ready(function() {...}); 尝试将jQuery包装在$(document).ready(function() {...}); just before the closing </body> tag like you normally do (a safe assumption). 像通常一样在结束</body>标记之前(一个安全的假设)。

Then use the async attribute on the <script> block. 然后在<script>块上使用async属性。

EXAMPLE: 例:

    <script src="https://cdn.jsdelivr.net/jquery/3.0.0-beta1/jquery.slim.min.js" async></script>
    $(document).ready(function() {
     $(result.html).hide().insertBefore('#addCommentContainer').slideDown();
       ....
    });
   </script>

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

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