다음 코드로 작업하는 전체 탭 시스템을 보유하고 있습니다. A LI 링크를 클릭하면 다른 div가 숨겨지고 선택한 div가 표시됩니다.Javascript 탭에 대한 링크
<script type="text/javascript">
//<![CDATA[
var tabLinks = new Array();
var contentDivs = new Array();
function init() {
// Grab the tab links and content divs from the page
var tabListItems = document.getElementById('tabs').childNodes;
for (var i = 0; i < tabListItems.length; i++) {
if (tabListItems[i].nodeName == "LI") {
var tabLink = getFirstChildWithTagName(tabListItems[i], 'A');
var id = getHash(tabLink.getAttribute('href'));
tabLinks[id] = tabLink;
contentDivs[id] = document.getElementById(id);
}
}
// Assign onclick events to the tab links, and
// highlight the first tab
var i = 0;
for (var id in tabLinks) {
tabLinks[id].onclick = showTab;
tabLinks[id].onfocus = function() { this.blur() };
if (i == 0) tabLinks[id].className = 'selected';
i++;
}
// Hide all content divs except the first
var i = 0;
for (var id in contentDivs) {
if (i != 0) contentDivs[id].className = 'tabContent hide';
i++;
}
}
function showTab() {
var selectedId = getHash(this.getAttribute('href'));
// Highlight the selected tab, and dim all others.
// Also show the selected content div, and hide all others.
for (var id in contentDivs) {
if (id == selectedId) {
tabLinks[id].className = 'selected';
contentDivs[id].className = 'tabContent';
} else {
tabLinks[id].className = '';
contentDivs[id].className = 'tabContent hide';
}
}
// Stop the browser following the link
return false;
}
function getFirstChildWithTagName(element, tagName) {
for (var i = 0; i < element.childNodes.length; i++) {
if (element.childNodes[i].nodeName == tagName) return element.childNodes[i];
}
}
function getHash(url) {
var hashPos = url.lastIndexOf ('#');
return url.substring(hashPos + 1);
}
//]]>
</script>
이 링크를 통해 탭을 열 수 있기를 원합니다. 그래서 내가 index.php#hello
을 방문하면 id가 hello
인 div에 링크됩니다. 간단히 링크를 방문해 보았지만 작동하지 않았습니다 ... 고맙습니다.
'getHash()'함수를 사용하는 대신에 왜'window.location.hash'를 사용하지 않을까요? 또한, 페이지로드시'showTab()'를 호출해야합니다. 페이지가로드 된 후 및 탭을 클릭 할 때만 페이지를 실행하는 것처럼 보이지 않습니다. –