a { text-decoration:none; color:#333; } #nav { overflow:hidden; } #nav ul li { background:#eaeaea; float:left; margin-right:10px; padding:10px 15px; list-style:none; position:relative; color:#181818; } #nav ul li.selected { background:#515151; } .selected { background:#515151; } #nav ul li.selected a { color:#fff; } #nav ul li a { text-decoration:none; display:block; } #nav span { width:0; height:0; border-right:10px solid transparent; border-top:10px solid red; position:absolute; top:0; left:0; display:none; } #nav li:first-child span { display:block; }
通过获取当前a元素的href属性,判断是否等于当前URL里面的链接URL,如果相当则添加选中后样式