Nel*_*lga 5 javascript xml optimization
我是javascript的新手,但已设法编写一个有效的xml函数:)
我希望有人能给我一个如何优化功能的简要介绍.目前每个州的天气都有不同的功能,但我希望我能以某种方式简化这一过程.
代码粘贴在这里:http://pastie.org/private/ffuvwgbeenhyo07vqkkcsw
任何帮助是极大的赞赏.谢谢!
编辑:添加两个XML Feed的代码示例:
$(d).find('location#sydney')
Run Code Online (Sandbox Code Playgroud)
值得怀疑.#sydney
表示具有属性值的元素,该元素具有sydney
模式类型ID
.在HTML中,由于DOCTYPE ,该id="..."
属性具有模式类型ID
.但是这个XML文件没有DOCTYPE,因此它的id="..."
属性没有模式类型ID
.因此getElementById('sydney')
不起作用,#sydney
因为选择器不应该工作.
它在实践中起作用,因为当你使用find()
jQuery时会回到它自己的'Sizzle'JavaScript选择器匹配器,它只是寻找id="..."
属性,就像它是HTML一样.但是Sizzle很慢,你不应该依赖这个实现细节.location[id=sydney]
对于XML文档,使用显式属性选择器更好.
var sydneyuv = sydneyuv += '<span>' + uvindex + '</span>' ;
Run Code Online (Sandbox Code Playgroud)
你在这里有一个多余的任务.您使用扩充分配+=
来添加内容sydneyuv
,然后再将结果分配给sydneyuv
.
此外,通常最好不要从输入值拼接HTML字符串.如果uvindex
有HTML特殊字符怎么办?(它可能不会,但是没有什么可以阻止你从中包含它们的网站.)如果没有HTML转义,你就会有HTML注入和潜在的XSS安全漏洞.始终使用DOM样式的方法,比如jQuery text()
和attr()
jQuery,或创建快捷方式:var $sydneyuv= $('<span/>', {text: uvindex});
优先于字符串吊索.
我希望我能以某种方式简化这一点.
当然.使其数据驱动:
var towns= ['sydney', 'melbourne', 'brisbane', 'perth', 'adelaide', 'darwin'];
var uvlevels= [
{uvlevel: 2, risk: 'Low', curcon: 'You can safely stay outdoors and use an SPF 15 moisturiser.'},
{uvlevel: 5, risk: 'Moderate', curcon: 'Wear protective clothing outdoors and use an SPF 15 or SPF 30 moisturiser.'},
{uvlevel: 7, risk: 'High', curcon: 'Wear protective clothing, limit your time outdoors and use an SPF 30 moisturiser.'},
{uvlevel: 10, risk: 'Very high', curcon: 'Use caution, limit exposure to the sun and use an SPF 30 moisturiser.'},
{uvlevel: 20, risk: 'Extreme', curcon: 'Use extreme caution, avoid exposure to the sun and use an SPF 30 moisturiser.'},
{uvlevel: null, risk: 'Unavailable', curcon: 'Information is currently unavailable.'}
];
Run Code Online (Sandbox Code Playgroud)
现在您可以用一个循环替换所有这些单独的语句:
$.each(towns, function() {
var $location= $(d).find('location[id='+this+']');
var uv= $location.find('index').text();
var shorttown= this.slice(0, 3);
$('#uv-'+shortttown).empty().append($('<span/>', {text: uv}));
$.each(uvlevels, function() {
if (this.uvlevel===null || uv<=this.uvlevel) {
$('#risk-'+shorttown).text(this.risk);
$('#curcon-'+shorttown).text(this.curcon);
return false;
}
});
});
Run Code Online (Sandbox Code Playgroud)
并且无论天气如何,大概都相似.
(我在HTML文档ID中使用完整的城镇ID,因此您不需要shorttown
黑客攻击.)
我建议您创建一个数组来保存 UV 元素 ID、后缀和气象站 ID ( stationid )
var areas = [
{id:'sydney',suffix:'syd',stationid:'YSSY'},
{id:'melbourne',suffix:'mel',stationid:'YMML'},
{id:'brisbane',suffix:'bri',stationid:'YBBN'},
{id:'perth',suffix:'per',stationid:'YPPH'},
...
]
Run Code Online (Sandbox Code Playgroud)
然后对于紫外线
// FUNCTION 1 - UV
function getUV() {
// BEGIN AUSTRALIAN UV FUNCTION
$.get('http://www.arpansa.gov.au/uvindex/realtime/xml/uvvalues.xml', function(d) {
//SYDNEY UV
$(areas).each(function(){
var area = this;
$(d).find('location#'+area.name).each(function(){
var $location = $(this);
var uvindex = $location.find('index').text();
var areauv = areauv += '<span>' + uvindex + '</span>' ;
$('#uv-'+area.suffix).empty().append($(areauv)); // empty div first
if (uvindex <= 2.0) {
$('#risk-'+area.suffix).empty().append('Low');
$('#curcon-'+area.suffix).empty().append('You can safely stay outdoors and use an SPF 15 moisturiser.');
} else if (uvindex <= 5.0) {
$('#risk-'+area.suffix).empty().append('Moderate');
$('#curcon-'+area.suffix).empty().append('Wear protective clothing outdoors and use an SPF 15 or SPF 30 moisturiser.');
} else if (uvindex <= 7.0) {
$('#risk-'+area.suffix).empty().append('High');
$('#curcon-'+area.suffix).empty().append('Wear protective clothing, limit your time outdoors and use an SPF 30 moisturiser.');
} else if (uvindex <= 10.0) {
$('#risk-'+area.suffix).empty().append('Very High');
$('#curcon-'+area.suffix).empty().append('Use caution, limit exposure to the sun and use an SPF 30 moisturiser.');
} else if (uvindex <= 20.0) {
$('#risk-'+area.suffix).empty().append('Extreme');
$('#curcon-'+area.suffix).empty().append('Use extreme caution, avoid exposure to the sun and use an SPF 30 moisturiser.');
} else {
$('#risk-'+area.suffix).empty().append('Unavailable');
$('#curcon-'+area.suffix).empty().append('Information is currently unavailable.');
}
});
});
// END OF AUSTRALIAN UV FUNCTION
});
}
Run Code Online (Sandbox Code Playgroud)
和天气
function getWeather() {
// BEGIN AUSTRALIA and NEW ZEALAND WEATHER FUNCTION
$(areas).each(function(){
var area = this;
$.get('http://api.wxbug.net/getLiveCompactWeatherRSS.aspx?ACode=XXXPRIVATEXXX&stationid='+area.stationid+'&unittype=1&outputtype=1', function(d){
$(d).find('weather').each(function(){
var $weatherinfo = $(this);
var degrees = $weatherinfo.find('temp').text().replace(/\.0$/i, "");
var conditions = $weatherinfo.find('current-condition').text();
var icon = $weatherinfo.find('current-condition').attr('icon').replace(/\.gif$/i, ".png").split('http://deskwx.weatherbug.com/')[1];
var temperature = temperature += '<span>' + degrees + '</span>' ;
$('#temp-'+area.suffix).empty().append($(temperature));
var winformation = winformation += '<span>' + conditions + '</span>' ;
$('#info-'+area.suffix).empty().append($(winformation));
var wicon = wicon += '<img src="' + icon + '" alt="Weather Unavailable" />' ;
$('#icon-'+area.suffix).empty().append($(wicon));
});
});
});
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
142 次 |
最近记录: |