最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JS Click not working - Stack Overflow

programmeradmin1浏览0评论

Very simple onclick not firing.

creating divs in a loop containing the classes:

class="person glyphicon glyphicon-user"

and i've tried selecting the class the only way i know

$('.person')click(function(){
console.log("something";
});

it just won't work, i can however print them all with the same selector, and i can fire onclick events on

$('body div').click

so its a div with class person, which leaves me clueless

thanks in advance.

edit: no browser errors, no action, nothing. the method copy pasted from my code:

$('.person').click(function() {
    console.log("something");
});

Very simple onclick not firing.

creating divs in a loop containing the classes:

class="person glyphicon glyphicon-user"

and i've tried selecting the class the only way i know

$('.person')click(function(){
console.log("something";
});

it just won't work, i can however print them all with the same selector, and i can fire onclick events on

$('body div').click

so its a div with class person, which leaves me clueless

thanks in advance.

edit: no browser errors, no action, nothing. the method copy pasted from my code:

$('.person').click(function() {
    console.log("something");
});
Share Improve this question edited Mar 1, 2015 at 14:18 Kasper Sølvstrøm asked Mar 1, 2015 at 14:10 Kasper SølvstrømKasper Sølvstrøm 2801 gold badge2 silver badges22 bronze badges 7
  • did you wrapped your click function from .ready ? – underscore Commented Mar 1, 2015 at 14:11
  • may be due to the missing dot before the click! – lshettyl Commented Mar 1, 2015 at 14:12
  • also a syntax error missing dot ... $('.person').click(). Use browser console to look for errors thrown – charlietfl Commented Mar 1, 2015 at 14:12
  • you missed "." $('.person').click – Vladu Ionut Commented Mar 1, 2015 at 14:13
  • Please show your code – Chris Pietschmann Commented Mar 1, 2015 at 14:15
 |  Show 2 more comments

7 Answers 7

Reset to default 5

You have multiple possible issues ( and assuming those are only typo's in your question):

  1. Ensure you have jQuery added to your html file and all other jQuery usage comes after that link.

  2. Ensure your JS is wrapped by a $(document).ready(function(){});

  3. "creating divs in a loop containing the classes:" This leads to believe you are creating them via a JS loop? If it is then you need to call your $('.person').click(function(){}); after the loop. Creating an event on an element type ( class in this case ) does not apply to future elements.

try to use jquery on instead of click

$( ".person" ).on( "click", function() {
console.log("something");
});

I am not sure if the element with class "person" is dynamically created. Either way, try:

$("body").on("click", ".person", function() {
    console.log("something");
});

If its not solved yet, Try this way too

$(document).on('click','.person',function(){
   console.log("something");
});

You missed a dot after the jquery selector :

$('.person')click(function(){
   // something
});

should be

$('.person').click(function(){
   // something
});

Also, ensure your click handler is declared after the div with class person

Edit, as it seems it is a typo only in your question and not in your code.

$(document).off('click','.person').on('click','.person',function(){ console.log('hello world');

$(document).ready(function(){
$('.person').click(function(){
alert('helloooo...')
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<button class="person">click me</button>

});

I am not sure whether you have done as below. But try this trouble shooting:

First of all, check if you imported jquery.min.jscorrectly, which includes correct path to jquery.

Then, check if your script loads only after jquery.min.js is loaded.

Once, again please make sure your code (or script) is fired or called only after jquery.min.js is loaded.

If this is ok, then, check if, in between any script fails to execute, as if any script fails to run in between, the code following it will be skipped. I mean to say like:

line in which your `jquery.min.js`
.
.
.
.
.
Some code that is error prone
.
.
.
.
.
And at last your script lies (which wont fire)
发布评论

评论列表(0)

  1. 暂无评论