简体   繁体   English

按钮onclick to click()jQuery

[英]Button onclick to click() jquery

I have a button that reacts to onclick() at the moment, but I want it to work with jquery. 目前,我有一个对onclick()有反应的按钮,但我希​​望它与jquery一起使用。

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

raisebuttonaction is in another .js This piece of code isn't working. raisebuttonaction在另一个.js中。这段代码不起作用。 Am I doing something wrong? 难道我做错了什么? edit1: 编辑1:

$(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. jQuery将无法找到该元素。 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. 将工作,因为a被脚本之前渲染。

You can also use $(document).ready or $() to execute your function when the DOM nodes load. 当DOM节点加载时,您还可以使用$(document).ready$()执行函数。

Or you can use jQuery.live . 或者,您可以使用jQuery.live

jQuery API jQuery API

  • ready Specify a function to execute when the DOM is fully loaded. ready 指定在DOM完全加载时要执行的函数。
  • live Attach a handler to the event for all elements which match the current selector, now and in the future. live 为现在和将来与当前选择器匹配的所有元素添加事件处理程序。

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

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