現時点で XML を読み取ることができる唯一の方法は、「解析」関数に関数を追加することですが、その関数の外に関数を追加できるようにしたいと考えています。[Match] ボタンをクリックしても何も起こりません。parse 関数内の関数は機能しますが。ほとんどの人は「ドキュメント」の代わりに「XML」を使用していますが、ドキュメントの代わりに xml を追加すると、「ReferenceError: xml が定義されていません」というエラーが表示されます。
解析関数の外側で xml に対して関数を実行したいと思います。ご協力いただきありがとうございます。
JS
$(document).ready(function(){
$.ajax({
url: 'data.xml',
dataType: "xml",
success: parse,
error: function(){alert("Error: Something wrong with XML");}
});
});
function parse(document){
$(document).find('Swatch').each(function(){
alert($(this).attr('name'));
});
}
$('#Match').on('click', function () {
$(document).find('Swatch').each(function(){
alert($(this).attr('name'));
});
});
XML
<?xml version="1.0" encoding="UTF-8"?>
<Fabric>
<Swatch name="2016" title="Ruby_Red" alt="Main" match="2004, 2005, 2020, 2026, 2035"></Swatch>
<Swatch name="2004" title="Spring_Yellow" alt="Knits"></Swatch>
<Swatch name="2005" title="Newport_Navy" alt="Knits"></Swatch>
<Swatch name="2006" title="Light_Purple" alt="Knits"></Swatch>
<Swatch name="2007" title="Royal_Blue" alt="Knits"></Swatch>
<Swatch name="2008" title="Ruby_Red" alt="Knits"></Swatch>
</Fabric>