<div class="myclass selected">
<h2 class = "title">#Content 1</h2>
</div>
<div class="myclass">
<h2 class = "title">#Content 2</h2>
</div>
<div class="myclass">
<h2 class = "title">#Content 3</h2>
</div>
<div class="myclass">
<h2 class = "title">#Content 4</h2>
</div>
我想在网址中显示为“http://example.com/#content-1”,并希望针对每个选项进行更新。使用jquery可以做到这一点吗?
答案 0 :(得分:2)
$('.myclass h2').click(function(){
window.location = 'http://example.com/' + $.text(this).toLowerCase().replace(/ /g, '-');
});
你有没有理由不使用常规链接?试试这个:
<div class="myclass selected">
<h2 class="title">
<a href="#content-1">#Content 1</a>
</h2>
</div>
<!-- Add the others here... -->
然后,只需添加此脚本:
jQuery(function() {
var hash = window.location.hash;
hash && $('.myclass').removeClass('selected').filter(function(){
return $(this).find('a').prop('href') == hash;
}).addClass('selected');
});
答案 1 :(得分:0)
更改此
var selected = $('.selected');
selected.removeClass('selected');
selected.hide();
var next = selected.next(".myclass");
到
var selected = $('.selected');
var next = selected.next(".myclass");
selected.removeClass('selected');
selected.hide();
您要删除课程selected
,然后再将其分配给next
..