Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery刚刚由ajax元素添加_Jquery - Fatal编程技术网

jQuery刚刚由ajax元素添加

jQuery刚刚由ajax元素添加,jquery,Jquery,php脚本是 它向0发出警报,因此无法看到新的div 如何让它看到新的div?您必须等到帖子完成后才能检查长度。这样做: $.post('includes/script.php', $(this).serialize(), function(data) { $('body').append(data); }); alert ($('#new').length) 你怎么称呼它?我在Firefox、Chrome和IE中收到警报1。请注意,“函数”在您的代码片段中拼写错误 $.post('

php脚本是

它向0发出警报,因此无法看到新的div


如何让它看到新的div?

您必须等到帖子完成后才能检查长度。这样做:

$.post('includes/script.php', $(this).serialize(), function(data) {
    $('body').append(data);
});

alert ($('#new').length)

你怎么称呼它?我在Firefox、Chrome和IE中收到警报1。请注意,“函数”在您的代码片段中拼写错误

$.post('includes/script.php', $(this).serialize(), function(data) {
    var newItem = $(data).appendTo('body');
    alert (newItem.length);
});

$(文档).ready(函数(){
addnew();
});
函数addnew(){
$('body')。追加('text');
警报($(“#新”).length)
}

我的坏习惯太简单了。更正,找到了。异步:false@Quio如果您能将代码移动到成功处理程序中,它将对您有效。Javascript是一种事件驱动语言。
<script type="text/javascript" src="jquery-1.4.2.js"></script>
<script type="text/javascript">

$(document).ready( function() {
addnew();
});

function addnew (){
    $('body').append('<div id="new">text</div>');
    alert ($('#new').length)
}
</script>