Home >Web Front-end >JS Tutorial >Based on jquery tab switching (preventing page refresh)_jquery

Based on jquery tab switching (preventing page refresh)_jquery

WBOY
WBOYOriginal
2016-05-16 17:53:401186browse

I found a lot of jquery effects on the Internet, and they were all like this, so I wrote one myself. To prevent tab switching from page refresh,
html code:

Copy code The code is as follows:












Project information

Customer

Job question




js code:
Copy code The code is as follows:

$(function ( ) {
$("div.tab").hide(); //Hide all
$("div.tabs a:first").addClass("current"); //The first element Select
$("div.tab:first").show(); //The first content is displayed
$("div.tabs a").click(function () {
$( "div.tabs a").removeClass("current"); //Remove the current style from all tabs
$(".tab").hide(); //Hide all
$(this) .addClass("current");
var activeTab = $(this).attr("href"); //Get div
$(activeTab).show();
});
//Get the
var url = window.location.href;
var reg = /#. /;
if (reg.test(url)) {//Contains #, The default is only one #, multiple # cases are not considered
//Hide all
$("div.tabs a").removeClass("current"); //Remove the current style from all tabs
$(".tab").hide(); //Hide all
var href = url.split('#')[1];
$("div.tabs [href=#" href "]").addClass("current");
$("#" href).show();
}
});

The code is very simple, The idea is also very clear, but very practical.
For example, in the above example, if you refresh the page and want to relocate to the second tab, you only need to re-specify xxx.aspx#kehu
Attached screenshot
Based on jquery tab switching (preventing page refresh)_jquery
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn