我的域中有一个xml,我想检索xml节点并从xml节点转换带有title链接(href url和target href属性)的div。
这是我的xml
<?xml version="1.0" encoding="UTF-8"?>
<xml>
<title>This is my Title</title>
<url>http://stackoverflow.com/</url>
<url_target>_blank</url_target>
</xml>
HTML:
<div id="title">
<a href="" target="" id="title_with_link"></a>
</div>
我正在使用jquery .load
<script>
var xmlpath = "myxml.xml";
$( "#title_with_link" ).load(xmlpath+ " title");
$( "#title_with_link a" ).load(xmlpath+ " url").attr('href'); // this is not working
$( "#title_with_link a" ).load(xmlpath+ " url_target").attr('target'); // this is not working
</script>
我的目标结果应该是
<a href="http://stackoverflow.com/" target="_blank" id="title_with_link">This is my Title</a>
我可以加载标题,但不能加载正确的href和target属性。
答案 0 :(得分:1)
/*
You can use get to load your XML content to the xmlDoc var
*/
var xmlDoc = $.parseXML( '<?xml version="1.0" encoding="UTF-8"?>'+
'<xml>'+
'<title>This is my Title</title>'+
'<url>http://stackoverflow.com/</url>'+
'<url_target>_blank</url_target>'+
'</xml>' );
var xml = $( xmlDoc );
$(".yo").text('<a href="'+xml.find("url").text()+'" target="'+xml.find("url_target").text()+'" id="'+xml.find("url").text()+xml.find("title").text()+'">'+xml.find("title").text()+'</a>');