I am now making a menu and use js to determine the address and add styles to the menu. If a certain address exists, I will add styles to the li of the menu. My current problem is that it feels very verbose to write out. I need to judge one by one. My current problem is that I want to write all these fields into one. Is it okay for you to judge without having to judge individually?
Search in a pile by judging whether a certain field exists in the url, and add the class if found. Thank you, God
var hrefof = window.location.href;
if (hrefof.indexOf('diannaodonghua') > 0) {
$(".menu > li ").eq(2).addClass('cur');
} else if (hrefof.indexOf('shejifb') > 0) {
$(".menu > li ").eq(2).addClass('cur');
} else if (hrefof.indexOf('gonggongguanxi') > 0) {
$(".menu > li ").eq(2).addClass('cur');
} else if (hrefof.indexOf('qiyecehua') > 0) {
$(".menu > li ").eq(2).addClass('cur');
} else if (hrefof.indexOf('jingjimaoyi') > 0) {
$(".menu > li ").eq(2).addClass('cur');
} else if (hrefof.indexOf('building') > 0) {
$(".menu > li ").eq(1).addClass('cur');
} else if (hrefof.indexOf('culture') > 0) {
$(".menu > li ").eq(1).addClass('cur');
} else if (hrefof.indexOf('news') > 0) {
$(".menu > li ").eq(1).addClass('cur');
} else if (hrefof.indexOf('recruit') > 0) {
$(".menu > li ").eq(4).addClass('cur');
}
Based on your thoughts
Can be extracted using regular expression
href
中的字段:/w+(?==w+)/g