jQuery .hide()有效,但不会.show()

时间:2018-02-22 21:30:22

标签: javascript jquery html

所以我试图只让一个部分一次启动,而另一部分启动时其他部分被隐藏。让我们说这是我的HTML:

MPPReader reader = new MPPReader();
String path2file = context.getFilesDir() + "/" +SAMPLE21_MPPX;
ProjectFile projectFile = reader.read(path2file); //<-- falling here

这是我的剧本:

<a href="#about" class="text-center" title="">About</a>

<div class="about">
    <h2>About</h2>
</div>

“关于”部分会立即隐藏,但是当我点击“关于”链接时,“关于”部分不会显示或返回到视图中。我在这里做错了吗?

3 个答案:

答案 0 :(得分:2)

您应该为锚元素添加一个id,例如id="about"

&#13;
&#13;
$(document).ready(function() {
    $('.about').hide();

    $('#about').on('click', function(e) {
        e.preventDefault();
        $('.about').show();
    });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a id="about" href="#about" class="text-center" title="">About</a>

<div class="about">
    <h2>About</h2>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:2)

您需要为点击事件设置正确的ID:

<a **id="about"** href="#about" class="text-center" title="">About</a>

<div class="about">
    <h2>About</h2>
</div>

然后这个脚本将起作用:

$(document).ready(function() {
    $('.about').hide();

    $('#about').on('click', function(e) {
        e.preventDefault();
        $('.about').show();
    });
});

答案 2 :(得分:-2)

you should put

$("a[href='#about']").on('click', function(e) {
...

或以其他方式为元素a分配ID。

<a href="#about" id="about" class="text-center" title="">About</a>

然后使用identity选择器调用它。

$('#about').on('click', function(e) {...
相关问题