按钮启动为活动状态
Button starts as active
我有几个按钮可以更改IFrame中的信息。我有它到按钮在单击按钮以显示其活动状态时更改类样式的位置,但想知道我将如何使按钮显示活动状态以开始......
< script language = "JavaScript"
type = "text/javascript" >
var nl;
var al;
function CngClass(obj) {
if (nl) nl.className = '';
obj.className = 'selected';
nl = obj;
}
function CngClass2(obj) {
if (al) al.className = '';
obj.className = 'selected';
al = obj;
} < /script>
<style> body {
margin: 0px;
position: absolute;
background: #cccccc;
}
a {
text-decoration: none;
color: #000000;
display: block;
vertical-align: middle;
width: 62px;
height: 20px;
padding-top: 5px;
}
a:hover {
text-decoration: none;
color: #fd5a1e;
background-color: #000000;
display: block;
width: 62px;
height: 20px;
padding-top: 5px;
vertical-align: middle;
}
.txt10 {
font-size: 10px;
font-family: arial, helvetica, verdana;
}
.txt11 {
font-size: 11px;
font-family: arial, helvetica, verdana;
}
.txt12 {
font-size: 12px;
font-family: arial, helvetica, verdana;
}
.txt16 {
font-size: 16px;
font-family: arial, helvetica, verdana;
}
.white {
color: #ffffff;
}
.bold {
font-weight: bold;
}
.pad3 {
padding-left: 3px;
}
.border {
border-bottom: 1px solid #000000;
}
.border1 {
border-bottom: 1px dashed #fd5a1e;
}
.btn {
text-decoration: none;
width: 62px;
height: 20px;
background-color: #cccccc;
color: #000000;
text-align: center;
vertical-align: middle;
display: block;
padding-top: 5px;
}
.selected {
background-color: #000000;
color: #fd5a1e;
width: 62px;
height: 20px;
text-align: center;
vertical-align: middle;
display: block;
padding-top: 5px;
border: 1px solid #fd5a1e;
}
</style>
<table border "0" cellpadding="0" cellspacing="0" style="width:500px; background-color:#cccccc">
<tr>
<td class="txt12 bold" colspan="4" align="center">National League</td>
<td class="txt12 bold" colspan="4" align="center">American League</td>
</tr>
<tr>
<td class="txt11 btn"><a onclick="CngClass(this);" href="nwest.html" target="national">West</a>
</td>
<td class="txt11 btn"><a onclick="CngClass(this);" href="ncentral.html" target="national">Central</a>
</td>
<td class="txt11 btn"><a onclick="CngClass(this);" href="neast.html" target="national">East</a>
</td>
<td class="txt11 btn"><a onclick="CngClass(this);" href="nwild.html" target="national">Wild</a>
</td>
<td class="txt11 btn"><a onclick="CngClass2(this);" href="awest.html" target="american">West</a>
</td>
<td class="txt11 btn"><a onclick="CngClass2(this);" href="acentral.html" target="american">Central</a>
</td>
<td class="txt11 btn"><a onclick="CngClass2(this);" href="aeast.html" target="american">East</a>
</td>
<td class="txt11 btn"><a onclick="CngClass2(this);" href="awild.html" target="american">Wild</a>
</td>
</tr>
<tr>
<td colspan="4">
<iframe src="nwest.html" name="national" frameborder="0" height="165" width="250">You need a Frames Capable browser to view this content.</iframe>
</td>
<td colspan="4">
<iframe src="awest.html" name="american" frameborder="0" height="165" width="250">You need a Frames Capable browser to view this content.</iframe>
</td>
</tr>
<td colspan="8" class="txt10" style="background-color:#cccccc; width:500px; height:25px;">
z-best record x-playoff team w-wild card team
</td>
</tr>
</table>
哦,
天哪,那真是一场噩梦,回到 5 岁。在编辑后尝试格式化上述答案的代码太麻烦了,将您的 html 改回原来的样子并将 JS 更改为这个。
var nl;
var al;
var Alinks = document.getElementsByTagName('a');
function CngClass(obj) {
for(var i=0;i < Alinks.length;i++){
if(Alinks[i].getAttribute('target')=="national"){
if(Alinks[i].className="selected"){
Alinks[i].className="";
}
}
}
if (nl) nl.className = '';
obj.className = 'selected';
nl = obj;
}
function CngClass2(obj) {
for(var i=0;i < Alinks.length;i++){
if(Alinks[i].getAttribute('target')=="american"){
if(Alinks[i].className="selected"){
Alinks[i].className="";
}
}
}
if (al) al.className = '';
obj.className = 'selected';
al = obj;
}
试试这个看看它是否正常,稍微改变了一下。不明白为什么你需要 2 个函数,所以我把它改成一个。考虑将其中一些内联样式添加到 css 中。可能有更好的方法,但这应该做到。### 编辑,忘记添加起始类...
function CngClass(obj,targ) {
var Alinks = document.getElementsByTagName('a');
var topboxes=document.getElementsByClassName('txt12');
for(var j=0;j<topboxes.length;j++){
topboxes[j].style.backgroundColor="#cccccc";
}
for(var i=0;i < Alinks.length;i++){
console.log(Alinks[i].className);
if(Alinks[i].className="selected"){Alinks[i].className="";
}
}
console.log(obj+' '+targ);
obj.className="selected";
document.getElementById(targ).style.backgroundColor="orange";
}
body {
margin: 0px;
position: absolute;
background: #cccccc;
}
a {
text-decoration: none;
color: #000000;
display: block;
vertical-align: middle;
width: 62px;
height: 20px;
padding-top: 5px;
}
a:hover {
text-decoration: none;
color: #fd5a1e;
background-color: #000000;
display: block;
width: 62px;
height: 20px;
padding-top: 5px;
vertical-align: middle;
}
.txt10 {
font-size: 10px;
font-family: arial, helvetica, verdana;
}
.txt11 {
font-size: 11px;
font-family: arial, helvetica, verdana;
}
.txt12 {
font-size: 12px;
font-family: arial, helvetica, verdana;
}
.txt16 {
font-size: 16px;
font-family: arial, helvetica, verdana;
}
.white {
color: #ffffff;
}
.bold {
font-weight: bold;
}
.pad3 {
padding-left: 3px;
}
.border {
border-bottom: 1px solid #000000;
}
.border1 {
border-bottom: 1px dashed #fd5a1e;
}
.btn {
text-decoration: none;
width: 62px;
height: 20px;
background-color: #cccccc;
color: #000000;
text-align: center;
vertical-align: middle;
display: block;
padding-top: 5px;
}
.selected {
background-color: #000000;
color: #fd5a1e;
width: 62px;
height: 20px;
text-align: center;
vertical-align: middle;
display: block;
padding-top: 5px;
border: 1px solid #fd5a1e;
}
<table border "0" cellpadding="0" cellspacing="0" style="width:500px; background-color:#cccccc">
<tr>
<td class="txt12 bold" colspan="4" align="center" id="national">National League</td>
<td class="txt12 bold" colspan="4" align="center"id="american">American League</td>
</tr>
<tr>
<td class="txt11 btn"><a onclick="CngClass(this,this.target)" class="selected" href="nwest.html" target="national">West</a>
</td>
<td class="txt11 btn"><a onclick="CngClass(this,this.target)" href="ncentral.html" target="national">Central</a>
</td>
<td class="txt11 btn"><a onclick="CngClass(this,this.target)" href="neast.html" target="national">East</a>
</td>
<td class="txt11 btn"><a onclick="CngClass(this,this.target)" href="nwild.html" target="national">Wild</a>
</td>
<td class="txt11 btn"><a onclick="CngClass(this,this.target)" class="selected" href="awest.html" target="american">West</a>
</td>
<td class="txt11 btn"><a onclick="CngClass(this,this.target)" href="acentral.html" target="american">Central</a>
</td>
<td class="txt11 btn"><a onclick="CngClass(this,this.target)" href="aeast.html" target="american">East</a>
</td>
<td class="txt11 btn"><a onclick="CngClass(this,this.target)" href="awild.html" target="american">Wild</a>
</td>
</tr>
<tr>
<td colspan="4">
<iframe src="nwest.html" name="national" frameborder="0" height="165" width="250">You need a Frames Capable browser to view this content.</iframe>
</td>
<td colspan="4">
<iframe src="awest.html" name="american" frameborder="0" height="165" width="250">You need a Frames Capable browser to view this content.</iframe>
</td>
</tr>
<td colspan="8" class="txt10" style="background-color:#cccccc; width:500px; height:25px;">
z-best record x-playoff team w-wild card team
</td>
</tr>
</table>
相关文章:
- 在Angular 2中布线期间保持零部件处于活动状态
- 如何使bxslider仅在移动视图中处于活动状态
- 悬停下拉菜单即使在鼠标移出后也保持活动状态
- 使用纯javascript而非jquery使所选选项卡处于活动状态并保持非活动状态
- 我如何为列出的选项卡元素编写一个Protractor测试,它会检查它是否's是否处于活动状态
- 返回上一页时,Javascript仍处于活动状态
- Angular+bootstrap ui,检查当前选项卡是否已处于活动状态
- 按钮处于活动状态,焦点无法在Mac FireFox上工作
- 禁用永久活动状态
- 单击我网站中的链接时打开新选项卡,保持当前选项卡处于活动状态
- 网页在等待 AJAX 响应时变为非活动状态
- 广告拦截处于活动状态,然后不要打开超链接到新标签
- 如何创建至少 1 个框处于活动状态的复选框列表
- 如何使按钮具有 :活动状态 1 秒
- JQuery UI 1.11 将选项卡设置为活动状态
- 如何获取链接以保持活动状态
- 限制 .live() 函数在 JavaScript 中变为活动状态的次数
- 按钮启动为活动状态
- 检测浏览器窗口是否处于活动状态,并在窗口再次处于活动状态后启动事件[JavaScript]
- 如何仅在启动选项卡处于活动状态时加载启动选项卡内容