JQuery点击做某事..单击警报后

JQuery onclick do something ... after click alert

本文关键字:单击 JQuery      更新时间:2023-09-26

我使用的是以下代码:

onclick="$('#default').click();"。。。如果成功地完成了某件事,有什么方法可以返回警报吗?

更新:

这里似乎有一个问题:

onclick="$('#default').click( function() { alert('clicked'); });"

这个语法有点错误。通常你会像这样使用jQuery的click()

HTML:

<a id="something">Text</a>

JavaScript:

$('#something').click( function() { alert('clicked'); });

更新:

即使是更新后的代码似乎也能工作,但这是一个非常糟糕的代码——您可能在javascript或DOM结构中的其他地方出现了一些错误。看见http://jsfiddle.net/HCQeN/1/

最好将jquery与onclick分开,比如:http://jsfiddle.net/ctUgp/

假设您的示例是:

<input type="button" id="myButton" onClick="$('#default').click()" />

你想要的是:

<input type="button" id="myButton" />
<script type="text/javascript">
$(document).ready(function(){
  // this code will run when the document has loaded
  // and all elements are in place
  $("#myButton").click(function(){
    // this code will be run when the user clicks on
    // the button we created above
    $('#default').click(); // this calls the click event on #default
    alert('Finished'); // now it is finished
  }); // close the click handler on #myButton
  $('#default').click(function(){
    // this code will be run when the user click on
    // the element with id "default" OR (in this case)
    // when the click event is triggered from clicking the
    // button above.
    alert('#default was clicked');
  }); // close the click handler on #default
}); // close the document.ready code block.
</script>

就像这个

$('#default').click( function() {
  alert('Handler for .click() called.');
} );

尝试:

onclick="$('#default').click(function() { alert('foobar'); });"