简体   繁体   English

使div在页面加载时隐藏,然后在单击按钮时取消隐藏

[英]Making a div hidden on page load and then unhidden when a button is clicked

I tried a technique that I read about somewhere, but it isn't working. 我尝试了一种我在某处读到的技术,但是它没有用。 The technique was to make a class called hidden that is removed on a function call. 该技术是使一个称为hidden的类在函数调用中删除。 Since I'm new to using JQuery, I think something might be wrong with how I was writing the onclick function. 由于我不熟悉JQuery,因此我认为编写onclick函数的方式可能有问题。 I'll post my attempt and then I'll appreciate it if you can help me correct my error(s). 我会发表尝试,如果您能帮助我纠正错误,我将不胜感激。

HTML: HTML:

    <button onclick="function() {$('newthreaddiv').removeClass('hidden');}">New Thread</button>
    <div class="boardbox hidden" id="newthreaddiv">
        <p>Username:<input class="threadipt" type="text"</p>
        <p>Password:<input class="threadipt" type="text"></p>
        <p>Title:<input class="threadipt" type="text"></p>
        <p>Content:</p>
        <button class="threadbutton">bold</button>
        <button class="threadbutton">italicize</button>
        <button class="threadbutton">underline</button>
        <button class="threadbutton">insert image</button>
        <textarea id="newthreadtxt"></textarea>
        <p><button onlick="phpfunction">Create Thread</button></p>
    </div> 

CSS: CSS:

div.boardbox
{
    background-color: #ccc;
    padding: 5px;
    margin: 20px;
}

div.hidden
{
    display: none;
}

function() {$('newthreaddiv') should be function() {$('#newthreaddiv') You are missing the # . function() {$('newthreaddiv')应该是function() {$('#newthreaddiv')您缺少#

Also, I'm not sure if this is just a typo or in your actual code, but you are missing a closing bracket ( > ) on your username input field. 另外,我不确定这只是拼写错误还是实际代码中的错误,但是您在用户名输入字段中缺少右括号( > )。

I recommend that you do not use inline javascript. 我建议您不要使用内联javascript。 Instead, create a listener for click events on the button. 而是,为按钮上的单击事件创建一个侦听器。

<button id="new_thread">New Thread</button>
<div class="boardbox hidden" id="newthreaddiv">
   ...
</div>
jQuery(function() {

    jQuery('#new_thread').on('click',function() {
        jQuery('#newthreaddiv').show();
    });

});

WORKING EXAMPLE 工作实例


METHOD 2 方法2

However, if you need the javascript to be inline, don't use a function: 但是,如果您需要内联JavaScript,请不要使用函数:

<button onclick="$('#newthreaddiv').removeClass('hidden');">New Thread</button>

WORKING EXAMPLE 工作实例


METHOD 3 方法3

You can use a function, but it will need to be an " Immediately-Invoked Function Expression " (and I don't see any purpose in this context): 您可以使用一个函数,但是它需要一个“ 立即调用的函数表达式 ”(在这种情况下,我看不到任何目的):

<button onclick="(function() {$('#newthreaddiv').removeClass('hidden');}())">New Thread</button>

WORKING EXAMPLE 工作实例

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

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