Javascript 错误 Uncaught TypeError: $(...).onclick is not a function
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35012231/
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
Javascript error Uncaught TypeError: $(...).onclick is not a function
提问by vanhiro
I am Javascript beginner and I got an error on my Javascript.
我是 Javascript 初学者,我的 Javascript 出错了。
javascript
javascript
$(function(){
$(".widgetselecterbtn").onclick(function(){
var index = $(".widgetselecterbtn").index(this);
$('.itemselected').addClass('hide');
$('.itemselected').eq(index).removeClass('hide');
});
});
html
html
<ul id="wighet">
<li><div><i class="fa fa-header fa-lg"></i><br><a class="widgetselecterbtn">heading</a></div></li>
<li><div><i class="fa fa-pencil fa-lg"></i><br><a class="widgetselecterbtn">text</a></div></li>
<li><div><i class="fa fa-youtube fa-lg"></i><br><a class="widgetselecterbtn">Youtube</a></div></li>
<li><div><i class="fa fa-quote-left fa-lg"></i><br><a class="widgetselecterbtn">quate</a></div></li>
<li><div><i class="fa fa-link fa-lg"></i><br><a class="widgetselecterbtn">link</a></div></li>
<li><div><i class="fa fa-picture-o fa-lg"></i><br><a class="widgetselecterbtn">image</a></div></li>
<li><div><i class="fa fa-twitter fa-lg"></i><br><a class="widgetselecterbtn">Twitter</a></div></li>
</ul>
<div class="ItemWidget01 itemselected hide">
</div>
<div class="ItemWidget01 itemselected hide">
</div>
<div class="ItemWidget01 itemselected hide">
</div>
<div class="ItemWidget01 itemselected hide">
</div>
<div class="ItemWidget01 itemselected hide">
</div>
<div class="ItemWidget01 itemselected hide">
</div>
<div class="ItemWidget01 itemselected hide">
</div>
The error is
错误是
Uncaught TypeError: $(...).onclick is not a function
未捕获的类型错误:$(...).onclick 不是函数
How can I solve this?
我该如何解决这个问题?
Thanks
谢谢
回答by Ibrahim Khan
onclick
is not a jQuery function. You should use click
instead.
onclick
不是 jQuery 函数。你应该click
改用。
$(".widgetselecterbtn").click(function() {
//do your task here
});