需要帮助使用XML和Javascript [英] Need help With XML and Javascript
问题描述
我创建了一个XML文件:
<?xml version =" 1.0"编码= QUOT; UTF-8英寸?>
$
<?xml-stylesheet type =" text / xsl" href =" myBooks.xsl"?>
<!DOCTYPE myBooks SYSTEM" myBooks.dtd">
<! - <?xml -stylesheet href =" mydogs.css" type =" text / css"?> - >
< myBooks>
< book>
< img> book1.jpeg< / img>
< title> Praire上的小房子< / title>
< author> Laura Ingalls Wilder< / author>
< descript>阳光亲吻的prarie在Ingalls家族周围延伸....< / descript>
< price>£ 6.99< / price>
< / book>
< book>
< img> book2.jpeg< / img>
< title>总店< / title>
< author> Matthew Shardlake< / author>
< descript>夏天,1545年。英格兰处于战争状态。亨利八世的入侵.....< / descript>
< price>£ 11.99< / price>
< / book>
< book>
< img> book3.jpeg< / img>
< title> Rebel Fay< / title>
< author> Barb Hendee< / author>
< descript>半精灵Leesil因需要拯救他的母亲而烧伤....< / descript>
< price>£ 10.99< / price>
< / book>
< / myBooks>
在HTM页面中,我使用javascript从XML文件中获取数据并将其放入htm页面。
当用户点击图书图片时,会打开一个窗口,其中显示一个"添加到购物车"的按钮
然而,'onclick图片上的"事件"应该发送图书名称的参数。我得到这个工作,除非我每次点击图书图像,即使我点击第一本和第二本书,最后一本书的名称也作为参数传递。
你怎么能纠正这个<这是htm页面中的Javascript。
< script language ="的JavaScript" type =" text / javascript">
if(window.XMLHttpRequest)
{//代码为IE7 +,Firefox,Chrome,Opera,Safari为
xmlhttp = new XMLHttpRequest();
}
其他
{//代码为IE6,IE5为
xmlhttp = new ActiveXObject(" Microsoft.XMLHTTP");
}¥b $ b
xmlhttp.open("GET","bookCatalogue.xml",false);
$
xmlhttp.send();
xmlDoc = xmlhttp.responseXML;
var t = 0;
x = xmlDoc.getElementsByTagName(" book")
var bookArr = new Array();
for(var i = 0; i< x.length; i ++)
{
for(var j = 1; j< x [i] .childNodes.length; j ++)
{
//这会得到书籍的名称
var currentNode = x [i] .childNodes [3];
var a = currentNode.firstChild.nodeValue;
//这将获取所有其他节点数据,即jpeg,价格,名称,描述
var Nnode = x [i] .childNodes [j];
var t = Nnode.firstChild.nodeValue;
if(j == 1)
{
///这可以获得图书的图像,其中onclick事件存储了图书的名称
document.write("< img src ='../img/" + t +"'border ='0'onclick ='showDetails(a)'/> ;< br>");
}
否则为
{
document.write("< p>" + t +"< p>");
}
j ++;
}
document.write("< br>< br>");
}
$
function showDetails(name)
{
提醒(姓名);
var bookURL =" pro_asp_details.htm"; $
detailsWindow = window.open(bookURL," bookDetails"," width = 400,height = 350");
}
$
< / script>
document.write("< img src ='.. / img /" + t +" 'border ='0'onclick ='showDetails( \"" + a +" \" )'/> < BR>");
I created an XML file :
<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="myBooks.xsl"?>
<!DOCTYPE myBooks SYSTEM "myBooks.dtd">
<!--<?xml-stylesheet href="mydogs.css" type="text/css"?>-->
<myBooks>
<book>
<img>book1.jpeg</img>
<title>Little House on the Praire</title>
<author>Laura Ingalls Wilder</author>
<descript>The sun-kissed prarie stretches out around the Ingalls family....</descript>
<price>£6.99</price>
</book>
<book>
<img>book2.jpeg</img>
<title>Head Store</title>
<author>Matthew Shardlake</author>
<descript>Summer, 1545. England is at war. Henry VIII’s invasion of.....</descript>
<price>£11.99</price>
</book>
<book>
<img>book3.jpeg</img>
<title>Rebel Fay</title>
<author>Barb Hendee</author>
<descript>Half-elven Leesil burns with the need to rescue his mother....</descript>
<price>£10.99</price>
</book>
</myBooks>
Within an HTM page i use javascript to get the data from the XML file and place it into the htm page.
When the use clicks on the book image a window opens with a button that says 'add to cart'
However, the 'onclick event' on the picture should send a parameter of the book name. i get this to work except that every time i click the book image the last book name is passed as a parameters even if i click the first and second book.
How can you correct this
here is the Javascript in the htm page.
<script language="JavaScript" type="text/javascript">
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","bookCatalogue.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
var t = 0;
x=xmlDoc.getElementsByTagName("book")
var bookArr=new Array();
for (var i = 0; i < x.length; i++)
{
for (var j = 1; j < x[i].childNodes.length; j++)
{
//this gets the names of the books
var currentNode = x[i].childNodes[3];
var a = currentNode.firstChild.nodeValue;
//this gets all the other node data i.e. jpeg,price,name,description
var Nnode = x[i].childNodes[j];
var t = Nnode.firstChild.nodeValue;
if (j == 1)
{
///this gets the image of the book with the onclick event storing the name of the book
document.write ("<img src= '../img/" + t + "' border='0' onclick ='showDetails(a)'/> <br>");
}
else
{
document.write ("<p>" + t + "<p>");
}
j++;
}
document.write ("<br><br>");
}
function showDetails(name)
{
alert(name);
var bookURL = "pro_asp_details.htm";
detailsWindow = window.open(bookURL,"bookDetails","width=400,height=350");
}
</script>
document.write ("<img src= '../img/" + t + "' border='0' onclick ='showDetails(\""+a+"\")'/> <br>");
这篇关于需要帮助使用XML和Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!