简体   繁体   English

getElementById&onclick按钮功能不起作用

[英]getElementById & onclick Button function won't work

Thanks alot to wiz kid & Axel Martinez ! 非常感谢wiz kid&Axel Martinez! Problem is solved ! 问题解决了!

works this way: 这样工作:

$(document).ready(function() {
    $("#info").on('click', function() {
        $(".infos").toggleClass("infos_sichtbar");
    });

i am trying to use my button with getElementById, but it won't work :( 我试图使用我的按钮与getElementById,但它将无法正常工作:(

The script is for a WebApplikation that is going to be a native App converted by PhoneGap. 该脚本适用于WebApplikation,它将是由PhoneGap转换的本机App。 I want to change the CSS class of an element with the script, so that my div called "infos" is getting visible. 我想用脚本更改元素的CSS类,以便我的div“infos”可见。 It works with this script: 它适用于此脚本:

    <script>
    $(document).ready(function() {
        $("button").on('click', function() {
            $(".infos").toggleClass("infos_visible");
        })
    });

    </script>

But the problem is, there are two buttons, one for informations that should get visible with the click and one for legal notices ... 但问题是,有两个按钮,一个用于信息,通过点击可以看到,一个用于法律通知......

I'm trying to get it work this way: 我试图让它以这种方式工作:

first attempt: 第一次尝试:

$(document).ready(function() {
        $.getElementById("info").on('click', function() {
            $(".infos").toggleClass("infos_sichtbar");
        })
    });

second attempt: 第二次尝试:

 $(document).ready(function() {
        $.getElementById("info").onclick = function() {
            $(".infos").toggleClass("infos_sichtbar");
                    }
                 /* $.getElementById("legalnotice").onclick = function() {
            $(".legalnotice").toggleClass("legalnotice_sichtbar");
        }*/
    });

When im using the script, the click on the button doesn't do anything. 当我使用脚本时,单击按钮不会执行任何操作。

Edit: 编辑:

HTML Code: HTML代码:

<body>

<div class="infos">Blub blub blub blub blub blub blub blub</div>

<header>
    <h1>App</h1>
</header>
<section>
    <h1>welcome!</h1>
    <p class="einleitung">intro</p>
<aside>
    <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. </p>
</aside><button id="info">Infos</button><button id="impress">Impressum</button>
</section>
<script src="zepto.js"></script>
<script src="app.js"></script>
<script>
window.scrollTo(0,1);
</script>

The script is implemented with the app.js file. 该脚本使用app.js文件实现。

Try using 尝试使用

$("#info") 

instead of 代替

$.getElementById("info")

ie

$(document).ready(function() {
        $("#info").on('click', function() {
            $(".infos").toggleClass("infos_sichtbar");
        });
});

If you are using jQuery, why not use the selectors like 如果您使用的是jQuery,为什么不使用像这样的选择器

$(document).ready(function() {
    $("#info").click(function() {
        $(".infos").toggleClass("infos_sichtbar");
    });
});

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

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