简体   繁体   English

jQuery / JavaScript函数仅可使用一次

[英]jquery/javascript function only works once

I have a very odd situation, I have looked all over and cannot find someone with a similar situation, and in my eyes, I don't see why this inst working. 我有一个非常奇怪的情况,我四处张望,找不到类似情况的人,而且在我眼中,我不明白为什么这个工作可行。 I'm trying to make a simple tab list with jQuery: 我正在尝试使用jQuery创建一个简单的选项卡列表:

I have a JS function as follows: 我有一个JS函数,如下所示:

function changeWindow(contentId) {
    $(".tabs").css("border-bottom","thin solid black");
    $("#" + contentId + "Tab").css("border-bottom","thick solid white");
    $(".content").hide();
    $("#" + contentId).show();
    $("#" + contentId + "Head").show();    //when I comment this line, all works well.
}

My html is: 我的html是:

<div id="header">
    <div id="tabs">
        <span onClick="changeWindow('browse')" id="browseTab" class="tabs"> Browse </span>
        <span onClick="changeWindow('collection')" id="collectionTab" class="tabs"> My Collection </span>
        <span onClick="changeWindow('play')" id="playTab" class="tabs"> Play! </span>
    </div>
    <div id="contentHeads">
        <div id="browseHead" class="content">
            some Html
        </div>
        <div id="collectionHead" class="content">
            some Html
        </div>
        <div id="playHead" class="content">
            some Html
        </div>
    </div>
</div>
<div id="gameField">
    <div id="browse" class="content">
        some Html
    </div>
    <div id="collection" class="content">
        some Html
    </div>
    <div id="play" class="content">
        some Html
    </div>
</div>

I dont think it matters much, but here is my CSS(I left some out): 我认为这没什么大不了的,但这是我的CSS(我省略了一些):

#tabs
{
    position: absolute;
    top: 10px;
    left: 10px;
    border-bottom: medium solid black;
}
.tabs
{
    border: thin solid black;
    padding-left: 50px;
    padding-right: 50px;
    margin-left: 10px;
    margin-right: 10px;
    cursor: pointer;
}
.content
{
    position: relative;
    height: 100%;
    overflow: hidden;
}

The curious thing is, this function will run once(ie i can click a tab) and everything works perfect. 奇怪的是,此功能将运行一次(即我可以单击一个选项卡),并且一切正常。 But after i click a tab, the CSS property cursor: pointer; 但是,当我单击选项卡后,CSS属性cursor: pointer; no longer did anything, and the JS function no longer works. 不再执行任何操作,并且JS函数不再起作用。 I have tested and other functions still run when called, just not this one. 我已经测试过,并且其他功能在调用时仍然可以运行,而不仅仅是这个。 After a bit of testing, I came to the conclusion that it is because of the last line in the JS function. 经过一些测试,我得出的结论是,这是由于JS函数中的最后一行。 When I comment it all works well(except that the Heads don't show). 当我发表评论时,一切正常(除了Heads不显示)。 I dont understand what is going on, i think it is an HTML problom, but have no clue. 我不明白发生了什么,我认为这是HTML问题,但是没有任何线索。 Does anyone know why this may be happening? 有谁知道为什么会这样?

Thanks in advance. 提前致谢。

The problem is that your "content" blocks come after the menu in the document, so when they're visible they cover the menu up. 问题在于您的“内容”块位于文档中的菜单之后 ,因此当它们可见时,它们会将菜单覆盖起来。

Here is a fixed jsfiddle to demonstrate. 这是一个固定的jsfiddle来演示。

I updated the CSS: 我更新了CSS:

.content {
    position: relative;
    height: 100%;
    overflow: hidden;
    margin-top: 40px;
    display: none;
}

I also added CSS to move the "gameField" stuff out of the way. 我还添加了CSS,以将“ gameField”内容移开。

It can be tricky to diagnose problems like this, but the developer tools ("inspector") generally make it a lot easier. 诊断这样的问题可能很棘手,但是开发人员工具(“检查器”)通常使它变得容易得多。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM