Jquery onclick div
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7034342/
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 on div
提问by Etienne No?l
I have a simple question. The following code works for all the tags:
我有一个简单的问题。以下代码适用于所有标签:
$('*').click(function(e) {
alert(1);
});
But, when I try this code for the div with id "content":
但是,当我为 id 为“content”的 div 尝试这段代码时:
$('#content').click(function(e) {
alert(1);
});
What am I doing wrong ?
我究竟做错了什么 ?
回答by Steve Robbins
Make sure it's within a document ready tagAlternatively, try using .live
确保它在文档就绪标签中,或者,尝试使用 .live
$(document).ready(function(){
$('#content').live('click', function(e) {
alert(1);
});
});
Example:
例子:
$(document).ready(function() {
$('#content').click(function(e) {
alert(1);
});
});
#content {
padding: 20px;
background: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<div id="content">Hello world</div>
As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers.
从 jQuery 1.7 开始,不推荐使用 .live() 方法。使用 .on() 附加事件处理程序。
$('#content').on( "click", function() {
alert(1);
});
回答by foxy
Nothing.
没有。
$('#content').click(function(e) {
alert(1);
});
Will bind to an existingHTML element with the ID set to content
, and will show a message box on click.
将绑定到ID 设置为 的现有HTML 元素content
,并在单击时显示一个消息框。
- Make sure that
#content
exists before using that code - That the ID is unique
- Check your Javascript console for any errors or issues
#content
在使用该代码之前确保存在- ID 是唯一的
- 检查您的 Javascript 控制台是否有任何错误或问题
回答by Mironor
May the div with id="content" not be created when this event is attached? You can try live() jquery method.
附加此事件时,是否可以不创建 id="content" 的 div?您可以尝试 live() jquery 方法。
Else there may be multiple divs with the same id or you just spelled it wrong, it happens...
否则可能有多个具有相同 id 的 div 或者您只是拼写错误,它会发生...
回答by ShankarSangoli
Wrap the code in $(document).ready()
method or $()
.
将代码包装在$(document).ready()
method 或 中$()
。
$(function(){
$('#content').click(function(e) {
alert(1);
});
});
回答by Ryosuke Hujisawa
js
js
<script
src="https://code.jquery.com/jquery-2.2.4.min.js"
integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
crossorigin="anonymous"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#div1").on('click', function(){
console.log("click!!!");
});
});
</script>
html
html
<div id="div1">div!</div>
回答by Amin Eshaq
Check out this fiddle... you're doing it correctly. Make sure the id is content and also check to see there are no other elements with the same id. If there are multiple elements with the same id, it will bind to the first one. That might be why you arn't seeing it.
看看这个小提琴......你做对了。确保 id 是内容,并检查是否有其他元素具有相同的 id。如果有多个具有相同 id 的元素,它将绑定到第一个。这可能就是你没有看到它的原因。