简体   繁体   English

jQuery将div附加到新div

[英]Jquery append div to new div

I have to append 2 div to new div: 我必须将2 div附加到新的div上:

<script type="text/javascript">
        $(function() {
            $( "#table-filter_wrapper" ).append($( "<div class='main_tbl_btm_info'></div>" ));
            $( "#table-filter_info" ).appendTo( $( ".main_tbl_btm_info" ));
            $( "#table-filter_paginate" ).appendTo( $( ".main_tbl_btm_info" ));
        });
</script>

The problem is that it can't find table-filter_wrapper at ready. 问题在于它无法立即找到table-filter_wrapper
When I use alert before append then it works. 当我在添加之前使用Alert时,它将起作用。
How can I get that table-filter_wrapper is loaded or not. 我如何获取或未加载table-filter_wrapper

you need to delete $ from your append like this: 您需要像这样从您的append删除$

<script type="text/javascript">
        $(function() {
            $( "#table-filter_wrapper" ).append( "<div class='main_tbl_btm_info'></div>");
            $( "#table-filter_info" ).appendTo(".main_tbl_btm_info" );
            $( "#table-filter_paginate" ).appendTo( ".main_tbl_btm_info");
        });
</script>

Your code works if the elements are available in the DOM: http://jsfiddle.net/XY7Sf/ 如果元素在DOM中可用,则您的代码有效: http : //jsfiddle.net/XY7Sf/

<div id="table-filter_wrapper"></div>
<div id="table-filter_info"></div>
<div id="table-filter_paginate"></div>

If they are not available in the DOM, you need to run this code when they are, maybe in an ajax callback somewhere? 如果它们在DOM中不可用,则需要在它们可用运行此代码,也许在某处的ajax回调中?

Try to target a wrapper div first or table and the use .find() to get the class you want to appentTo like: 尝试首先将包装器div或表作为目标,并使用.find()获得要appentTo的类,例如:

var a = $(".wrapper").find(".main_tbl_btm_info")
$( "#table-filter_paginate" ).appendTo(a);

You want something like this, right ? 你想要这样的东西,对吗?

Demo JSFIDDLE 演示JSFIDDLE

JS: JS:

$(function() {
            $( "#table-filter_wrapper" ).append($( "<div class='main_tbl_btm_info'>main_tbl_btm_info</div>" ));

    $( ".main_tbl_btm_info" ).append($( "<div id='table-filter_info'>table-filter_info</div>" ));

    $( ".main_tbl_btm_info" ).append($( "<div id='table-filter_paginate'>table-filter_paginate</div>" ));

}); });

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

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