我正在解析一个XML文件,解析后,我希望我的函数connectPort(source,dest)将两个盒子连接在一起。我的代码布局如下:
<script>
var connectPort = function(src, dest){ //function definition..}
$(document).ready(function(){
$.ajax({
type: "GET",
url: someurl.xml,
dataType: "xml",
success: function(xml) {
//parsing..
html = ' <div class = "classA"';
html += ' blah blah blah'; //this is where I do the HTML
// and display it on the page
}
});
});
//is this where I should call the function?
$(window).load(function(){
var $xml = $(xml); //Is this the correct call?
$xml.find('personRepresentation').has('outputId').has('inputId').each(function () {
var $br = $(this);
connectPort($br.find('personRepId').text(), $br.find('outputId').text());
})
});
</script>
connectPort()函数在代码中的当前位置不起作用,它在我将所有内容作为document.write()之前工作,不确定是否有所作为。
答案 0 :(得分:0)
a ajax代表异步。所以你应该将xml结果传递给回调。尝试这样的事情:
$(function(){
var connectPort = function(src, dest){
console.log(src, dest);
}
$.get( 'someurl.xml', function(xml){
var $xml = $(xml);
$xml.find('personRepresentation').has('outputId').has('inputId').each(function () {
var $br = $(this);
connectPort($br.find('personRepId').text(), $br.find('outputId').text());
});//end each
}).fail(function() {
alert( "failed xml get" );
});
});//end ready