gpt4 book ai didi

javascript - 将 javascript 和 jquery 与 XSL 转换或函数参数混合时遇到问题?

转载 作者:行者123 更新时间:2023-12-02 19:56:51 25 4
gpt4 key购买 nike

我试图让页面上的导航按钮加载内容文件,但其中一些文件是 XML,需要使用 XSL 转换进行样式设置。我过去使用 w3schools.com 的 XSL On the Client 代码取得了成功,因此我在下面进行了调整。但是,有些东西似乎不适用于我的 javascript 和 jquery 混合,或者也许我没有正确传递参数? Firebug 告诉我它无法加载“xsl”变量。这个语法看起来有什么问题吗?我对此很陌生,不太明白我做错了什么。

<script>
$(document).ready(function(){
$("#aboutTextButton").click(function(){
$('#contentContainer').load('about-text.html');
});
$("#aboutProjectButton").click(function(){
$('#contentContainer').load('about-project.html');
});
$("#catalogButton").click(function(){
displayResult("catalog.xml","xml-style2.xsl");
});
$("#transcriptionButton").click(function(){
displayResult("behaviour.xml","xml-style2.xsl");
});
});

//script to transform XML, adapted from w3schools.com
function loadXMLDoc(dname)
{
if (window.XMLHttpRequest)
{
xhttp=new XMLHttpRequest();
}
else
{
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET",dname,false);
xhttp.send("");
return xhttp.responseXML;
}

function displayResult(xml, xsl)
{
// code for IE
if (window.ActiveXObject)
{
ex=xml.transformNode(xsl);
document.getElementById("example").innerHTML=ex;
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
{
xsltProcessor=new XSLTProcessor();
xsltProcessor.importStylesheet(xsl);
resultDocument = xsltProcessor.transformToFragment(xml,document);
document.getElementById("contentContainer").appendChild(resultDocument);
}
}
</script>
</head>

<body>

<div id="headerImg">
<img src="cover.jpg">
</div>

<div id="menu">
<ul>
<li><a id="aboutTextButton">About the Text</a></li>
<li><a id="aboutProjectButton">About the Project</a></li>
<li><a id="catalogButton">Browse the Images</a></li>
<li><a id="transcriptionButton">Read the Text</a></li>
</ul>
</div>

<div id="contentContainer">
</div>

最佳答案

您从不调用 loadXMLDoc,因此 displayResults 函数不会处理 xml 文档,而是处理您传递给该函数的文件名。 displayResult 的第一行应该是:

xml = loadXMLDoc(xml);
xsl = loadXMLDoc(xsl);

那么你的代码应该可以工作。

关于javascript - 将 javascript 和 jquery 与 XSL 转换或函数参数混合时遇到问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8497963/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com