简体   繁体   中英

Button onclick to click() jquery

I have a button that reacts to onclick() at the moment, but I want it to work with jquery.

$('#nupp1').click(function() {
    raiseButtonAction(realPlayer, gameState);
});

raisebuttonaction is in another .js This piece of code isn't working. Am I doing something wrong? edit1:

$(document).ready(function() {
    $('#nupp1').click(function() {
        raiseButtonAction(realPlayer, gameState);
    });
});

Assuming you have this:

<head>
    <scripts />
</head>
<body>
    <a id="nupp1"></a>
</body>

You code will not work. jQuery will not be able to find the element. The element must exists for that function to work. So

<a id="nupp1"></a>
<script />

Will work because a is being rendered before the script.

You can also use $(document).ready or $() to execute your function when the DOM nodes load.

Or you can use jQuery.live .

jQuery API

  • ready Specify a function to execute when the DOM is fully loaded.
  • live Attach a handler to the event for all elements which match the current selector, now and in the future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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