jQuery如果再使用URL解析器插件,则必须有一个更优雅的解决方案 [英] jQuery if then else using URL parser plugin, there must be a more elegant solution

查看:99
本文介绍了jQuery如果再使用URL解析器插件,则必须有一个更优雅的解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在这个颇具特色的网站上的第一篇文章,所以如果在其他地方进行了讨论(如果有的话,我找不到).

我正在使用在这里找到的JQuery URL解析器插件: http://projects.allmarkedup.com/jquery_url_parser/

我可以让它做我想做的事,但是代码效率很低.单击相关标题时,将打开一个隐藏的div集合.我使用的是URL解析器,因此,如果从另一个页面单击链接,则可以看到相关的div.

我的代码如下:

if 
(jQuery.url.attr('anchor') == 'question1'){
    $('#answer1').show();
}
else if
(jQuery.url.attr('anchor') == 'question2'){
    $('#answer2').show();
}
else if
(jQuery.url.attr('anchor') == 'question3'){
    $('#answer3').show();
}
else if
(jQuery.url.attr('anchor') == 'question4'){
    $('#answer4').show();
}
else if
(jQuery.url.attr('anchor') == 'question5'){
    $('#answer5').show();
}
else if
(jQuery.url.attr('anchor') == 'question6'){
    $('#answer6').show();
}
else if
(jQuery.url.attr('anchor') == 'question7'){
    $('#answer7').show();
}
else if
(jQuery.url.attr('anchor') == 'question8'){
    $('#answer8').show();
};

如您所见,这是一个漫长的过程.我真正想要做的是将URL末尾的数字作为数字并将其附加到#answer上,以使事情更加紧凑.我正在尝试将(jQuery.url.attr('anchor'))的结果分配给一个变量,但在实现此功能时遇到了一些麻烦.任何帮助将不胜感激!

解决方案

var match = jQuery.url.attr('anchor').match(/^question([0-9]+)$/);
if (match && match.length > 0) {
    $('#answer' + match[1] ).show();
}

已更新.

This is my first post on this rather spiffing website so go easy on me if this has been discussed elsewhere (I can't find it if it has).

I'm using the JQuery URL parser plugin found here: http://projects.allmarkedup.com/jquery_url_parser/

I can get it to do what I want but the code is rather inefficient. I have a collection of hidden div's that are opened when the relevant heading is clicked. I am using the URL parser so that if a link is clicked from another page the relevant div is visible.

My code looks like this:

if 
(jQuery.url.attr('anchor') == 'question1'){
    $('#answer1').show();
}
else if
(jQuery.url.attr('anchor') == 'question2'){
    $('#answer2').show();
}
else if
(jQuery.url.attr('anchor') == 'question3'){
    $('#answer3').show();
}
else if
(jQuery.url.attr('anchor') == 'question4'){
    $('#answer4').show();
}
else if
(jQuery.url.attr('anchor') == 'question5'){
    $('#answer5').show();
}
else if
(jQuery.url.attr('anchor') == 'question6'){
    $('#answer6').show();
}
else if
(jQuery.url.attr('anchor') == 'question7'){
    $('#answer7').show();
}
else if
(jQuery.url.attr('anchor') == 'question8'){
    $('#answer8').show();
};

As you can see this is rather long winded. What I really want to be able to do is take the number at the end of the URL and append it to #answer so that things are far more compact. I was trying to assign the result of (jQuery.url.attr('anchor') to a variable but I'm having a bit of trouble accomplishing this. Any help would be greatly appreciated!

解决方案

var match = jQuery.url.attr('anchor').match(/^question([0-9]+)$/);
if (match && match.length > 0) {
    $('#answer' + match[1] ).show();
}

Updated.

这篇关于jQuery如果再使用URL解析器插件,则必须有一个更优雅的解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆