简体   繁体   English

HTML按钮onclick事件不起作用

[英]Html button onclick event not working

I want html button "News"to show my div "New" which is hidden here's my code( it's not working right now): 我想让html按钮“新闻”显示我的div“新”,这是隐藏的,这是我的代码(目前无法正常工作):

<div id = "Buttons">
    <button id="News">News</button><br>
    <button id="Marks">Marks</button><br>
    <button id="Averages">Averages</button><br>
    <button id="Forum">Forum</button><br>
</div>  
<div id="New">
    <p id="Newer">Text</p>
</div>

#New{
        display: none;
        width: 500px;
        height: 500px;
        background-color: grey;
}

$(document).ready(function(){
    $("#News").click(function() {
        $("#New").show();
    });
});
<div id = "Buttons">
    <button id="News">News</button><br>
    <button id="Marks">Marks</button><br>
    <button id="Averages">Averages</button><br>
    <button id="Forum">Forum</button><br>
</div>  
<div id="New">
    <p id="Newer">Text</p>
</div>

<style>          <---------------this
#New{
        display: none;
        width: 500px;
        height: 500px;
        background-color: grey;
}
</style>          <---------------this

<script>          <---------------this
$(document).ready(function(){
    $("#News").click(function() {
        $("#New").show();
    });
});
</script>         <---------------this

You can't just put styles and scripts as text into your HTML page. 您不能只将样式和脚本作为文本放入HTML页面。 You have have to tell the browser what it should do with the code. 您必须告诉浏览器它应该如何处理代码。

First, you need to wrap your code in the appropriate tags: 首先,您需要将代码包装在适当的标签中:

<style>
#New{
    display: none;
    width: 500px;
    height: 500px;
    background-color: grey;
}
</style>
<script>
$(document).ready(function(){
    $("#News").click(function() {
        $("#New").show();
    });
});
</script>

Second, you need to make sure JQuery is included before your script tag: 其次,您需要确保在脚本标记之前包含JQuery:

<script src="http://code.jquery.com/jquery-3.1.1.min.js"></script>
<div id = "Buttons">
    <button id="News">News</button><br>
    <button id="Marks">Marks</button><br>
    <button id="Averages">Averages</button><br>
   <button id="Forum">Forum</button><br>
</div>  
<div id="New">
    <p id="Newer">Text</p>
</div>

I'm assuming you're example is shortened up. 我假设你的榜样被缩短了。 Meaning, I'm assuming you have a <script> along with a <style> . 意思是,我假设您有一个<script>和一个<style> If not, those are necessary. 如果没有,那是必要的。 Also, I would seriously think about re-naming some of your stuff. 另外,我会认真考虑重新命名您的某些内容。 For example, anytime you have a wrapper that represents a series of similar elements, use a class name instead of id . 例如,只要您有一个表示一系列相似元素的包装器,就可以使用类名而不是id For example, I'd replace your div with a class instead of id like so: 例如,我将用一个类而不是id替换div ,如下所示:

<div class="buttons">
    <button id="news">News</button><br>
    <button id="marks">Marks</button><br>
    <button id="averages">Averages</button><br>
    <button id="forum">Forum</button><br>
</div>  

<div id="New">
    <p id="Newer">Text</p>
</div>

That being said, your code works as is if you add the appropriate tags like so: 话虽如此,如果您添加如下所示的适当标签,您的代码将按原样工作:

<div class="buttons">
    <button id="news">News</button><br>
    <button id="marks">Marks</button><br>
    <button id="averages">Averages</button><br>
    <button id="forum">Forum</button><br>
</div>  

<style>
    #New{
        display: none;
        width: 500px;
        height: 500px;
        background-color: grey;
    }
</style>

<script>
    $(document).ready(function(){
        $("#News").click(function() {
            $("#New").show();
        });
    });
</script>

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

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