使用js更改默认选项卡和页面
Changing the default tab and page with js
我是网页设计的初学者。我需要一些专业人士来帮我解决这个问题。
下面是我用来创建TABS的JS。我已经更改了默认选项卡,但是我仍然不知道如何更改默认选项卡页面。请帮忙!!!
<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["order"].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 = 'active';
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>
这是选项卡列表
<ul id="tabs">
<li><a href="#features">Product Features</a></li>
<li><a class="selected" href="#order">Order UNIFY Sutures</a> </li>
<li><a href="#spec">Specifications</a></li>
<li><a href="#testi">Testimonials</a></li>
<li><a href="#overview">Suture Overview</a></li>
</ul>
在此处更改代码
// Hide all content divs except the first
var i = 0;
for ( var id in contentDivs ) {
if ( i != 0 ) contentDivs[id].className = 'tabContent hide';
i++;
}
if子句if ( i != 0 )
中的零定义了活动选项卡页。
您还可以查看jqueryui选项卡,它可以实现您想要的功能并且易于使用。
将showTab函数替换为:
function showTab() {
var selectedId = getHash( this.getAttribute('href') );
selectTab(selectedId);
return false;
}
function selectTab(selectedId) {
// 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 = 'active';
contentDivs[id].className = 'tabContent';
} else {
tabLinks[id].className = '';
contentDivs[id].className = 'tabContent hide';
}
}
}
然后,要选择一个特定的选项卡,请调用以下命令:
selectTab('spec');
编辑:更改selectTab('#spec');选择Tab("spec");
JSFiddle
相关文章:
- CKeditor:更改对话框中的默认选择选项
- 如何从外部页面激活非默认引导选项卡
- AngularJS ng选项在选择一个选项后删除了默认的空白值
- 或者在表单上选择默认选项文本(选择1)
- Angularjs-设置不带ng选项的select的默认值
- 在AngularJS Dropdownlost中设置默认文本选项
- 使用jQuery向“选择选项”添加默认属性
- Angular:如何使用ngRepeat并设置默认单选按钮选项
- 选择非空选项后,默认选项消失
- 使用 ajax 填充选择时没有选定的默认选项的问题
- javascript中选项对象的默认值
- 我在angular JS中选择的默认选项
- 如何设置助手的默认(全局)选项
- 将默认选项添加到ng-if
- 默认按表列选择选项(使用Javascript)
- HTML选择:将默认值设置为给定值,而不是从选项列表中选择
- 如何在“挖空.js”中单击后显示选项默认标题
- 避免筛选异步 ng 选项默认值
- 显示所有选项的 ng 选项默认选项
- jQuery-插件选项默认扩展()