使用jquery加载外部html文件并访问其内容


loading external html file using jquery and accessing it content

我需要访问另一个HTML文件中的TABLE中的一些TD值。我真的不确定是什么方法可以帮助我找到一个有效的解决方案。目前我想使用jquery访问它。我所想的解决这个问题的方法就是将整个外部HTML文件加载到我的主文件中的DIV中,并在TD中查找我最喜欢的一个,并将其内容打印到我的主要TABLE TD中。我似乎无法解决这个问题。请帮帮我!我的主要PHP文件:

<?php
...
...
$loading_transmitter = $_POST['transmitter']; 
?> 
<script>  
$("#result").load('<?php echo $loading_transmitter; ?>',function(){
    var main_proc_temp = $(table:nth-child(2).tr:nth-child(1).td:nth-child(3)).text();#table loaded from the external file
    alert(main_proc_temp);#it should alert: "OK" but alets: "Undefined"
});
</script>

加载的文件的内容可以通过这种方式使用吗?因为在将它加载到页面上(进展顺利)后,我检查了源代码,发现resultdiv仍然是空的。

我认为您没有tr and td class,所以从selectors中删除.,然后进行检查。

此外,您应该在responsefind您的table,如

比如

$("#result").load('<?php echo $loading_transmitter; ?>',function(data){
    var main_proc_temp = $(data).find('table:nth-child(2) tr:nth-child(1) td:nth-child(3)').text();
    alert(main_proc_temp);
});

如果您使用的是PHP,您可以不在这些值更改时将其写入数据库并以这种方式访问它们吗?