我正在使用JQuery和XML。
我有以下XML格式。
资源XML:
<?xml version="1.0"?>
<root xmlns:link="http://www.example.com/tridion">
<data name="LoginAccount" tcm="tcm:233-191754" type="Text">
<value>Login to your Account</value>
</data>
<data name="Airport" tcm="tcm:233-191754" type="Text">
<value>Airport</value>
</data>
<data name="BusinessClass" tcm="tcm:233-191754" type="Text">
<value>Business</value>
</data>
</root>
现在我已经得到了JQuery,我希望先加载这些值,然后在页面中进一步使用它们。
JQuery代码示例:
// Dialog
$('#LoginLink').click(function(){
$('#Login').dialog({
autoOpen: true,
width: 450,
modal: true,
title: 'Login to your Account'
});
if($('#Login').is(':visible')) {
hideSelect();
} else {
showSelect();
}
});
在上面的jquery代码中,文本‘登录到您的帐户’应该来自my,因为我的应用程序是多语言的。
我希望在JQuery中创建这样的函数,在该函数中,我将传递名称属性值,它将从获取实际值,例如。
getDataFromResourceFile('LoginAccount');应该向您的帐户显示‘登录’
请给我建议!
发布于 2010-12-19 02:05:58
使用它将您的xml文档加载到变量中:
var xmlData;
$.ajax({
type: "GET",
url: "resources.xml",// your xml file path
dataType: "xml",
success: function(xml) {
xmlData = xml;// set the returned xml into a global variable
}
});
然后
function getDataFromResourceFile(resourceKey)
{
return $(xmlData).find('data[name=' + resourceKey + '] value').text();
}
这将用于XML文件。在使用之前,您可能必须检查XML数据是否已加载到变量(XmlData)中。
编辑
编辑以从服务器加载xml,如果找不到它。
<script type="text/javascript">
var xmlData;
function getDataFromResourceFile(key)
{
if (xmlData == null)
{
$.ajax({
type: "GET",
url: "resources.xml",
dataType: "xml",
success: function (xml)
{
xmlData = xml;
return $(xmlData).find('data[name=' + key + '] value').text();
}
});
}
else
{
return $(xmlData).find('data[name=' + key + '] value').text();
}
}
</script>
https://stackoverflow.com/questions/4482332
复制相似问题