使用XML和Javascript/Jquery填充页面,检查url是否与XML标记相同
我需要创建一个javascript函数,该函数将基于url编写页面,因此基本上我正在尝试创建一个javascript函数来检查url,并从中找到相应的xml项 这背后的原因是html页面可以被复制、重命名和xml更新,页面将填充xml表中的所有内容 请让我知道这是否是完全不正确的方法,当然还有更好的方法。谢谢 XML代码::使用XML和Javascript/Jquery填充页面,检查url是否与XML标记相同,javascript,jquery,xml,if-statement,auto-populate,Javascript,Jquery,Xml,If Statement,Auto Populate,我需要创建一个javascript函数,该函数将基于url编写页面,因此基本上我正在尝试创建一个javascript函数来检查url,并从中找到相应的xml项 这背后的原因是html页面可以被复制、重命名和xml更新,页面将填充xml表中的所有内容 请让我知道这是否是完全不正确的方法,当然还有更好的方法。谢谢 XML代码:: <!--XML INFORMATION--> <channel> <design> <motion><!-
<!--XML INFORMATION-->
<channel>
<design>
<motion><!--design content-->
<item><!--//////////POST//////////POST//////////POST//////////-->
<tag>/portfolio_dec.html</tag>
<!--RSS INFORMATION-->
<title>Decoze</title>
<link>http://payamrajabi.com/portfoliotest.html</link>
<description><img src="http://payamrajabi.com/thumbs/small_jump.png" title="JUMP!." /></description>
<!--PROJECT CONTENT-->
<project><!--project start-->
<titl>TITLE</titl><!--project title-->
<dsc>PROJECT DESCRIPTION</dsc><!--project description-->
</project><!--project end-->
</item><!--//////////END//////////END//////////END//////////-->
</motion>
</design>
</channel>
/公文包_dec.html
诱饵
http://payamrajabi.com/portfoliotest.html
标题
项目说明
JAVASCRIPT:
$(document).ready(function(){
$.ajax({
type: "GET",
url: "code/content9.xml",
dataType: "xml",
success: function(xml) {
var xpathname = window.location.pathname;
var xproject = $(xml).find('tag').text();
if (xpathname == xproject) {
$(xml).find('item').children('tag').text(xpathname).each(function(){
var ttl = $(this).find('titl').text();
$('<p>'+ttl+'</p>').appendTo('h1#ttl');
});
$(xml).find('item').children('tag').text(xpathname).each(function(){
var dsc = $(this).find('dsc').text();
$('<p>'+dsc+'</p>').appendTo('h1#ttl');
});
} else {
PUT ERROR MESSAGE HERE
}
}
});
});
$(文档).ready(函数(){
$.ajax({
键入:“获取”,
url:“code/content9.xml”,
数据类型:“xml”,
成功:函数(xml){
var xpathname=window.location.pathname;
var xproject=$(xml).find('tag').text();
if(xpathname==xproject){
$(xml).find('item').children('tag').text(xpathname).each(function(){
var ttl=$(this.find('titl').text();
$(“”+ttl+””).appendTo('h1#ttl');
});
$(xml).find('item').children('tag').text(xpathname).each(function(){
var dsc=$(this.find('dsc').text();
$(“”+dsc+“”).appendTo('h1#ttl');
});
}否则{
把错误信息放在这里
}
}
});
});
以及HTML:
<html>
<head>
<script type="text/javascript" src="code/jquery-1.3.1.js"></script>
<script type="text/javascript" src="code/project/project_design.js"></script>
</head>
<body>
<h1 id="ttl"></h1>
<p id="dsc"></p>
</body>
</html>
任何帮助都将不胜感激,我对javascript/jquery/xml非常陌生,在这方面我真的遇到了麻烦。我想做的主要事情是有一个xml文件来填充一个站点,每个项目都是一个新页面的内容,在本例中是一个公文包项目
干杯
威廉嗯。。。恐怕您不太了解jquery是如何工作的 您的代码应该如下所示:
var xpathname = window.location.pathname;
var xitem = $(xml).find('tag:contains(' + xpathname + ')').parent();
if (xproject.length != 0) {
$('#ttl').append('<p>' + xitem.find('titl').text() + '</p>');
$('#dsc').append('<p>' + xitem.find('dsc').text() + '</p>');
}
else {
$('#err').text('The page you requested does not exist');
}
$(function(){
$.get(
'http://jsbin.com/afiwa',
function(xml){
var hash = window.location.hash.substring(1);
if ($.trim(hash) === '') {
showError();
return;
}
var xitem = $(xml).find('urlname:contains(' + hash + ')').parent();
if (xitem.length != 0) {
$('#ttl').append(xitem.find('titl').text());
$('#dsc').append( xitem.find('dsc').text());
}
else {
showError();
}
function showError() {
$('#err').text('The page you requested does not exist');
}
}
);
});