简体   繁体   English

我的Javascript不起作用。不知道它是函数,我是如何调用它,我使用的CSS或者是什么

[英]My Javascript doesn't work. Don't know if it's the Function, how I'm calling it, the CSS I used or what

My function: 我的功能:

function ChangeStep(id)
{
    var i = 1;

    // hide all other tabs:
    while(i<10) {
        var divID = 'tabs' + i;
        if (divID !== null) {
            document.getElementById(divID).className += " hide";
        }
        i++;
    }

    // show this one
    document.getElementById(id).className += " show";
}

How I'm calling it: 我怎么称呼它:

<div id="prev"><img src="img/prv.png" onClick="ChangeStep('tabs1'); return false;"></div>
<div id="next"><img src="img/nxt.png" onClick="ChangeStep('tabs2'); return false;"></div>

The Divs I want to show/hide: 我要显示/隐藏的Div:

<div id="tabs1" class="hide"><h1>Step 1</h1></div>
<div id="tabs2" class="show"><h1>Step 2</h1></div>

My Css: 我的Css:

.hide { visibility: hidden; }
.show { visibility: visible; }

Basically what I want to do is: When I click Next or Previous, show the corresponding "Tab" by hiding all the others and showing this. 基本上我想要做的是:当我单击“下一个”或“上一个”时,通过隐藏所有其他人并显示此对话来显示相应的“选项卡”。 This should be done by adding the class "Hide" or "Show" depending on if it is to be hidden or visible. 这应该通过添加类“隐藏”或“显示”来完成,具体取决于它是隐藏还是可见。

You are always appending class names, so they will (after being shown once) be members of both the show and hide classes and both rulesets will apply (in the normal cascade order). 您总是附加类名,因此它们(在显示一次之后)将成为showhide类的成员,并且两个规则集都将应用(以正常的级联顺序)。

If you don't need to maintain any other classes, you can replace your append operator += with a simple assignment: = 如果您不需要维护任何其他类,则可以使用简单赋值替换追加运算符+==

If you do need to maintain other classes on the elements, then you can use classList instead. 如果确实需要在元素上维护其他类,则可以使用classList

document.getElementById(divID).classList.remove('show');
document.getElementById(divID).classList.add('hide');

If you need to support Old-IE then you can use a polyfill (which will probably run regex over the classList ) or an API that provides similar functions. 如果您需要支持Old-IE,那么您可以使用classList (可能在classList运行regex)或提供类似功能的API。 Most common JS libraries (such as YUI and jQuery) have one built in. 最常见的JS库(例如YUI和jQuery)内置了一个。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的机器人不和谐不起作用。 我不知道为什么 - My bot discord doesn't work. I don't know why 我为自动化 web 组件创建而创建的 Javascript function 不起作用。 怎么了? - The Javascript function i created to automate web component creation doesn't work. What's wrong? 我在 css 中应用了一些属性,但是由于 bootstrap 的 css 属性,我的属性没有被应用。 我不知道我应该输入什么 - I'm applying some properties in css But my properties aren't applied because of bootstrap's css property. I don't know what i should type 我的 innerHTML 不起作用,我不知道为什么? - My innerHTML doesn't work and I don't know why? 我的for循环不起作用,我也不知道为什么 - my for loop doesn't work and I don't know why 我不知道我做错了什么,它不起作用,我正在尝试列出任务清单 - i don't know what i did wrong, it doesn't work, i'm trying to make a task list 我不知道我做错了什么,但是我的带有 js 和 css 的 .html 不会显示必须显示的内容 - I don't know what I'm doing wrong but my .html with js and css won't show what is has to show 我的 JavaScript 不起作用。 我怎么解决这个问题? - My JavaScript doesn't work. How can I solve this problem? 我的 Javascript AVG 计算器不工作。 我该如何解决? - My Javascript AVG Calculator doesn't work. How can i fix? 为什么这段代码行不通。 我在库p5中使用javascript - Why doesn't this code work. I'm using javascript with the library p5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM