Javascript JQuery onclick 做某事...点击后警报
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8727005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JQuery onclick do something ... after click alert
提问by Satch3000
I am using this code:
我正在使用此代码:
onclick="$('#default').click();"
... is there any way to return an alert of something if it's done sucessfully?
onclick="$('#default').click();"
...如果成功完成,有什么方法可以返回警报吗?
Update:
更新:
There seems to be a proble here:
这里似乎有一个问题:
onclick="$('#default').click( function() { alert('clicked'); });"
回答by kontur
That syntax is a bit off. Usually you'd use jQuery's click()
like this:
那个语法有点不对。通常你会click()
像这样使用 jQuery :
HTML:
<a id="something">Text</a>
HTML:
<a id="something">Text</a>
JavaScript:
$('#something').click( function() { alert('clicked'); });
JavaScript:
$('#something').click( function() { alert('clicked'); });
Update:
更新:
Even your updated code seems to work, but it is very bad code like that - you might have some error somewhere else in your javascript, or in the DOM structure. See http://jsfiddle.net/HCQeN/1/
即使您更新后的代码似乎也能工作,但它是非常糟糕的代码 - 您的 javascript 或 DOM 结构中的其他地方可能有一些错误。见http://jsfiddle.net/HCQeN/1/
It would be much better to seperate the jquery from the onclick, like: http://jsfiddle.net/ctUgp/
将 jquery 与 onclick 分开会更好,例如:http: //jsfiddle.net/ctUgp/
回答by Thomas Clayson
Lets say your example is:
让我们说你的例子是:
<input type="button" id="myButton" onClick="$('#default').click()" />
What you want is:
你想要的是:
<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>
回答by nikans
just like this
像这样
$('#default').click( function() {
alert('Handler for .click() called.');
} );
回答by nav
Try:
尝试:
onclick="$('#default').click(function() { alert('foobar'); });"