我使用一系列if语句在论坛上使用以下方法操作css:
if(location.href.match(/(showforum=3)/i) != null) {
$(document).ready(function(){
$("#topimg").addClass("announce");
});}
代码工作得很好,但是每一个以3开头的展示论坛都会显示这个图像,除非我不是这样编码的。因此,我的问题是,如何使我的位置更准确,这样它只会改变3,而不是3x?甚至可以使用这种编码吗?
发布于 2014-09-05 04:49:06
更改正则表达式,以便也检查“值的边界”:
var pattern = /showforum=3(?=&|$)/i;
if (pattern.test(location.href)) {
...
}
请注意测试表达式中伴随的更改:如果只需要了解某些字符串是否与模式匹配,则应该使用regexp.test(string)
语法,而不是string.match(regexp) !== null
。
发布于 2014-09-05 04:52:21
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
if( getParameterByName("showforum") == 3 ){
//........
}
参考
https://stackoverflow.com/questions/25678751
复制相似问题