我的代码是这样的,以便在同一浏览器中从一个选项卡创建另一个选项卡。
function newTab()
{
var form1 = document.createElement("form");
form1.id = "newTab1"
form1.method = "GET";
form1.action = "domainname"; //My site address
form1.target = "framename"; //Browser tab name
document.body.appendChild(form1);
form1.submit();
}
上面的代码可以正常工作以创建新选项卡。当单击“ MyNewTab”链接到主页时
<a href="javascript:newTab()" style=" margin-left:15px;" id="newTab"> MyNewTab </a>
我再次尝试将主页切换到新打开的选项卡。这次需要从首页切换到新标签页,而无需重新加载。但是此newtab内容已重新加载。
如何通过单击“ MyNewTab”链接获取选项卡名称并获得新打开的选项卡的焦点?
您需要在javascript cookie中设置当前选定的标签,并始终在页面加载时读取此cookie并设置标签
您可以在newTab()函数中设置cookie:
function newTab()
{
var form1 = document.createElement("form");
form1.id = "newTab1"
form1.method = "GET";
form1.action = "domainname"; //My site address
form1.target = "framename"; //Browser tab name
$.cookie("currentTab", "framename");//set the selected tab name in cookie
document.body.appendChild(form1);
form1.submit();
}
然后,页面加载事件:
var tabName=$.cookie("currentTab")//get the current tab name from cookie.
if(tabName==null){
tabName="default tab name"//at first time the cookie will be null,
//so you need to assign a default value if the cookie is null
}
//set this tab as selected
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句