Add Active Class to Href Menu
•
Ditulis oleh Tim Pasti Oke
Include Query String / Parameter
$(function(){
var current = location.pathname
$('div.sidebar-nav ul a').each(function(){
var $this = $(this)
// if the current path is like this link, make it active
// console.log("current = " + current)
// console.log("href = " + $this.attr('href'))
// console.log("Hasil = " + $this.attr('href').indexOf(current))
console.log(this.href)
console.log(location.href)
//if($this.attr('href').indexOf(current.replace("/","")) !== -1 && current != '/'){
if(this.href == location.href && current != '/'){
console.log(this.href)
console.log($(this).parent().parent().parent().parent())
$this.parent().parent().addClass('show')
$this.parent().parent().parent().parent().addClass('show')
$this.addClass('active')
// $(this).parent().addClass('show')
// $(this).parent().parent()
//console.log($(this).parent().parent().parent())
}
})
})
Without Parameter / Query String
<script>
//PENGATURAN MENU
$(function(){
var current = location.pathname
$('div a').each(function(){
var $this = $(this)
// if the current path is like this link, make it active
// console.log("current = " + current)
// console.log("href = " + $this.attr('href'))
// console.log("Hasil = " + $this.attr('href').indexOf(current))
if($this.attr('href').indexOf(current.replace("/","")) !== -1 && current != '/'){
//console.log(this.href)
console.log($(this).parent().parent().parent().parent())
$this.parent().parent().addClass('show')
$this.parent().parent().parent().parent().addClass('show')
// $this.addClass('active')
// $(this).parent().addClass('show')
// $(this).parent().parent()
//console.log($(this).parent().parent().parent())
}
})
})
Alternative Without Parameter
//PENGATURAN MENU
$(function(){
var current = location.protocol + '//' + location.host + location.pathname
$('div.sidebar-nav ul a').each(function(){
var $this = $(this)
// if the current path is like this link, make it active
// console.log("current = " + current)
// console.log("href = " + $this.attr('href'))
// console.log("Hasil = " + $this.attr('href').indexOf(current))
console.log(this.href)
console.log(location.href)
//if($this.attr('href').indexOf(current.replace("/","")) !== -1 && current != '/'){
if(this.href == current && current != '/'){
console.log(this.href)
console.log($(this).parent().parent().parent().parent())
$this.parent().parent().addClass('show')
$this.parent().parent().parent().parent().addClass('show')
$this.addClass('active')
// $(this).parent().addClass('show')
// $(this).parent().parent()
//console.log($(this).parent().parent().parent())
}
})
})
Butuh solusi instan siap pakai?
Dapatkan modul & script server production-ready langsung di web store kami.
Komentar
Belum ada komentar. Jadilah yang pertama memberikan komentar!