jQueryを使用してXMLファイルから属性値を取得しようとしていますが、undefinedが返され、画像はロードされません。jQueryを使用してXMLファイルからデータを取得
私のHTMLファイルには、answer
の<div>
と、showInfo
の<button>
の1つがあります。
xml code:
<?xml version="1.0"?>
<student studentNum="5678">
<image>images/mypic.jpg</image>
</student>
私のjQueryコード:
$(document).ready(function(){
$.ajax({
url:'stu.xml',
data:{},
type:'GET',
dataType:'xml',
success: function(resp){
$('#showInfo').click(function() {
var myInfo = $(resp).find('student');
myInfo.each(function(index, obj) {
var myNum = ($(obj).find('student').attr('studentNum'));
var myImg = $(obj).find('image').text();
$('#answer').append("<p> " + "Student Number: " +myNum+
"<img src='" + myImg + "'/><br/>");
});
});
});
可能な重複[jQueryのを使用してXMLを解析するには?](https://stackoverflow.com/questions/7228141/how-to-parse-xml-using-jquery) – miken32