听起来您提供的链接与您的要求没有任何关系。如果我正确理解您的问题,您正在尝试解析 xml 数据并将其插入您的 HTML 表中。您可以为此使用 jQuery。
您应该只创建一个包含所有图像的所有数据的 xml 文件,而不是为每个图像创建一个 xml 文件。这样你只需要发出一个 http 请求。
这是一个例子:
XML
<?xml version="1.0"?>
<images>
<image id="img1">
<details>
Here is a bunch of details for image 1.
</details>
</image>
<image id="img2">
<details>
Here is a bunch of details for image 2.
</details>
</image>
</images>
HTML
<table id="table">
<tr>
<td>
<img src="img1.png" alt="img" />
<div id="img1" class="details">
</div>
</td>
<td>
<img src="img2.png" alt="img" />
<div id="img2" class="details">
</div>
</td>
</tr>
</table>
jQuery
$(function(){
// Make an ajax call to the server to get the xml file.
$.ajax({
// The URL where the xml file exists.
url: 'data.xml'
})
// Jquery's callback that handles a failed request. I'm just logging the message to the console.
.fail( function( jqXHR, textStatus){ console.log( textStatus ); } )
// Jquery's callback that handels success. Here Im executing the insertData() function.
.done( insertData( ) );
function insertData( ){
// The .done() method is expecting a funciton as it's parameter so we are returning one.
// .done() executes that function passing the requested data in.
// I named it data.
return function( data ){
// You can parse the xml data using jquery selection methods. I'm using find().
// This gets all the <image> object from our xml.
var images = $(data).find('image');
// For each <image> object do the following.
images.each(function(){
// Find the id of the <image> object.
var id = $(this).attr('id');
// Find the text of the <image> object.
var text = $(this).find('details').text();
// I chose to associate the table elements in the html with the xml data via an id attribute.
// The data in the xml has the same id as the details div in my html.
// Here I just select the details div and insert the text that we got from above.
$( '#'+id ).text( text );
});
};
};
});