javascript - 选项卡在外部单击时不在焦点

标签 javascript jquery html css jsp

我有一个包含四个选项卡的网页。它工作正常,除了当我在包含选项卡及其内容的 div 外部单击时,事件选项卡上的焦点丢失,这是不需要的。

代码

<body>
<img src="images/2.png" style="width: 950px; height: 60px;">
<div style="margin-top: -50px; margin-left: 5px; position: absolute; font-size: 32px; color: green;">Test
</div>
<div style="width: 69px; height: 57px; margin-left: 951px; margin-top: -58px;">
  <a href="#" title="Logout"><img src="images/logout.png" style="width: 78px; height: 73px;"></a>
</div>
<div style="margin-top: 100px;">
  <ul id="tabs" class="tab">
    <li><a id="load" href="#" class="tablinks" onclick="openTab(event, 'Reference')">Reference</a></li>
    <li><a href="#" class="tablinks" onclick="openTab(event, 'TestStrategy')">Test Strategy</a></li>
    <li><a href="#" class="tablinks" onclick="openTab(event, 'TestCase')">Test Case</a></li>
    <li><a href="#" class="tablinks" onclick="openTab(event, 'TestSummary')">Test Summary</a></li>
  </ul>

  <img src="images/line.png" style="margin-top: -14px; margin-left: 6px; width: 700px; height: 4px; position: absolute;">

  <div id="Reference" class="tabcontent">
    <p>Document_1.docx</p>
    <hr align="left">
    <p>Document_2.docx</p>
    <hr align="left">
    <p>Document_3.docx</p>
    <hr align="left">
  </div>

  <div id="TestStrategy" class="tabcontent">
    <p>TS_1.docx</p>
    <hr align="left">
    <p>TS_2.docx</p>
    <hr align="left">
  </div>

  <div id="TestCase" class="tabcontent">
    <p>TC_1.xlsx</p>
    <hr align="left">
    <p>TC_2.xlsx</p>
    <hr align="left">
    <p>TC_3.xlsx</p>
    <hr align="left">
    <p>TC_4.xlsx</p>
    <hr align="left">
    <p>TC_5.xlsx</p>
    <hr align="left">
  </div>

  <div id="TestSummary" class="tabcontent">
    <p>Summary_1.docx</p>
    <hr align="left">
    <p>Summary_2.pdf</p>
    <hr align="left">
    <p>Summary_3.ppt</p>
    <hr align="left">
  </div>
</div>
</body>

这是一个fiddle .

如何解决?

最佳答案

看看附件片段

$(document).ready(function(){
	alert("Loaded");
	document.getElementById('Reference').style.display = "block";
	document.getElementById('load').focus();
  
  

});

function openTab(evt, tabName) {
	//alert(evt + "-" + tabName);
    // Declare all variables
    var i, tabcontent, tablinks;

    // Get all elements with class="tabcontent" and hide them
    tabcontent = document.getElementsByClassName("tabcontent");
    for (i = 0; i < tabcontent.length; i++) {
        tabcontent[i].style.display = "none";
    }

    // Get all elements with class="tablinks" and remove the class "active"
    tablinks = document.getElementsByClassName("tablinks");
    for (i = 0; i < tabcontent.length; i++) {
        tablinks[i].className = tablinks[i].className.replace(" active", "");
    }

    // Show the current tab, and add an "active" class to the link that opened the tab
    document.getElementById(tabName).style.display = "block";
    evt.currentTarget.className += " active";
}
body {
	font-family:"Segoe UI";
	font-size:75%;
	width:1024px;
/*	height: 760px;*/
	line-height:1;
	outline:none;
	overflow:auto;
	background:url(../images/1.png);
	margin:0 auto;
	background-repeat: no-repeat;
	background-size: 1024px 760px;
    background-attachment: fixed;
    background-position: center; 
/*    background-color: #cccccc;*/
}

.active{
background:#333f50 !important;
}
hr {
    height: 10px;
    width: 69.2%;
/*    margin-left: -7px;*/
    border: 0;
    box-shadow: 0 10px 6px -10px #bababa inset;
}

/* Style the list */
ul.tab {
    list-style-type: none;
    margin-left: 5px;
    padding: 0;
    overflow: hidden;
/*    border: 3px solid red;*/
    width: 485px;
    background-color: #f1f1f1;
}

/* Float the list items side by side */
ul.tab li {float: left;}

/* Style the links inside the list items */
ul.tab li a {
    display: inline-block;
    color: white;
    background-color: #ADB9CA;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
    transition: 0.3s;
    font-size: 17px;
    font-family:Segoe UI;
    border: 1px solid #CCC;
/*    margin-left: 2px;*/
}

/* Change background color of links on hover */
ul.tab li a:hover {background-color: #ddd;}

/* Create an active/current tablink class */
ul.tab li a:focus, .active {background-color: #333F50;}

/* Style the tab content */
.tabcontent {
    display: none;
    padding: 6px 12px;
/*    border: 1px solid #ccc;*/
    border-top: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img src="images/2.png" style="width: 950px; height: 60px;">
<div
	style="margin-top: -50px; margin-left: 5px; position: absolute; font-size: 32px; color: green;">Test
</div>
<div style="width: 69px; height: 57px; margin-left: 951px; margin-top: -58px;">
<a href="#" title="Logout"><img src="images/logout.png" style="width: 78px; height: 73px;"></a>
</div>
<div style="margin-top: 100px;">
<ul id="tabs" class="tab">
 <li><a id="load" href="#" class="tablinks" onclick="openTab(event, 'Reference')">Reference</a></li>
    <li><a href="#" class="tablinks" onclick="openTab(event, 'TestStrategy')">Test Strategy</a></li>
    <li><a href="#" class="tablinks" onclick="openTab(event, 'TestCase')">Test Case</a></li>
    <li><a href="#" class="tablinks" onclick="openTab(event, 'TestSummary')">Test Summary</a></li>
  
</ul>
</div>
<img src="images/line.png"
	style="margin-top: -14px; margin-left: 6px; width: 700px; height: 4px; position: absolute;">

<div id="Reference" class="tabcontent">
<p>Document_1.docx</p>
<hr align="left">
<p>Document_2.docx</p>
<hr align="left">
<p>Document_3.docx</p>
<hr align="left">
</div>

<div id="TestStrategy" class="tabcontent">
<p>TS_1.docx</p>
<hr align="left">
<p>TS_2.docx</p>
<hr align="left">
</div>

<div id="TestCase" class="tabcontent">
<p>TC_1.xlsx</p>
<hr align="left">
<p>TC_2.xlsx</p>
<hr align="left">
<p>TC_3.xlsx</p>
<hr align="left">
<p>TC_4.xlsx</p>
<hr align="left">
<p>TC_5.xlsx</p>
<hr align="left">
</div>

<div id="TestSummary" class="tabcontent">
<p>Summary_1.docx</p>
<hr align="left">
<p>Summary_2.pdf</p>
<hr align="left">
<p>Summary_3.ppt</p>
<hr align="left">
</div>
</div>

关于javascript - 选项卡在外部单击时不在焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37967422/

相关文章:

html - 哪个是 webpack 最好的模板引擎?

html - CSS center a <ul> tag within div 问题

javascript - AngularJs Jasmine 单元测试因意外请求而失败

javascript - 如何在jquery中隐藏数据过滤器?

javascript - 包含脚本时 react "document not defined"

javascript - 从动态生成 id 的文本框中选取值

javascript - div被添加重复

javascript - 清理可搜索字符串 JS

JavaScript 无法在 onclick 属性中找到函数

java - 如何构建同一 Web 应用程序的在线和离线版本