最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Button onclick to click() jquery - Stack Overflow

programmeradmin2浏览0评论

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);
    });
});

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);
    });
});
Share Improve this question edited Mar 31, 2011 at 15:39 rosscj2533 9,3237 gold badges41 silver badges57 bronze badges asked Mar 28, 2011 at 18:04 nilsnils 3353 gold badges5 silver badges16 bronze badges 7
  • show the full code and markup, when you assign the handler? – vittore Commented Mar 28, 2011 at 18:05
  • Where did you put that piece of code? It should be in a "ready" handler. – Pointy Commented Mar 28, 2011 at 18:05
  • Where are the variables realPlayer and gameState ing from? – gen_Eric Commented Mar 28, 2011 at 18:05
  • Is raisebuttonaction script being included before this bind script? – BrunoLM Commented Mar 28, 2011 at 18:06
  • Where/when is this code being run? Part of $(document.ready)? – AlG Commented Mar 28, 2011 at 18:06
 |  Show 2 more ments

1 Answer 1

Reset to default 3

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.
发布评论

评论列表(0)

  1. 暂无评论