admin管理员组

文章数量:1404073

I've three divs. Each div must increment its counter val upon clicking them.

HTML:

<body>
    <content>
        <div id="box1" class="v1" onclick="counter('box1')";>A : <span class="num">0</span></div>
        <div id="box2" class="v2" onclick="counter('box2')">B: <span class="num">0</span></div>
        <div id="box3" class="v3" onclick="counter('box3')">C: <span class="num">0</span></div>
    </content>
</body>​

Javascript:

  function counter(id){
     var id = document.getElementById(id);
     $('#id').click(function () {
          update($("span"));
     });
  }

  function update(j) {
     var n = parseInt(j.text(), 10);
     j.text(n + 1);
  }

Here is the code demo

I've three divs. Each div must increment its counter val upon clicking them.

HTML:

<body>
    <content>
        <div id="box1" class="v1" onclick="counter('box1')";>A : <span class="num">0</span></div>
        <div id="box2" class="v2" onclick="counter('box2')">B: <span class="num">0</span></div>
        <div id="box3" class="v3" onclick="counter('box3')">C: <span class="num">0</span></div>
    </content>
</body>​

Javascript:

  function counter(id){
     var id = document.getElementById(id);
     $('#id').click(function () {
          update($("span"));
     });
  }

  function update(j) {
     var n = parseInt(j.text(), 10);
     j.text(n + 1);
  }

Here is the code demo

Share Improve this question edited Mar 5, 2012 at 21:06 Nealv 6,8848 gold badges61 silver badges91 bronze badges asked Mar 5, 2012 at 19:39 PriyaPriya 9213 gold badges11 silver badges24 bronze badges 1
  • 3 You'll need to be a bit more specific about the problems you are having. Posting a link to the fiddle is great but without more info you're basically asking people "please debug this for any errors". The question will be quickly closed in the current state – JaredPar Commented Mar 5, 2012 at 19:43
Add a ment  | 

8 Answers 8

Reset to default 4

You are doing a lot of work that jQuery would do for you. If you change your class to simply box and use the ID's to style your content, you can do the whole thing like this:

<body>
    <content>
        <div id="box1" class="box">A: <span class="num">0</span></div>
        <div id="box2" class="box">B: <span class="num">0</span></div>
        <div id="box3" class="box">C: <span class="num">0</span></div>
    </content>
</body>

 

$( function() {
    $('.box').click( function() {
        var num = $(this).find('.num');
        num.text( parseInt(num.text()) + 1 );
    });       
});

Example: http://jsfiddle/ddvQU/1/

Some thoughts:

  • If a style is unique to a single element (now and in the future), you should be using IDs. Styles that are (or will be) mon to multiple elements should use classes.

  • Using inline javascript onclick='blah()' is more difficult to manage, as it is not as easy to debug, does not allow for code reuse, and forces you to make updates in lots of places when you change code. It also makes you do nasty things like escaping quotes.

  • var id = document.getElementById(id); <= The whole reason we have jQuery is so that we don't have to do this. Simply do $('#'+id). (ok, maybe not the whole reason, but one of them).

  • You don't need to do the above if you attach a jQuery handler to your class of elements (see the first bullet). The handler will already have a reference to the object, even if it doesn't even have an ID.

  • I would use .on() instead of .click(), but as you look to be new to jQuery, get this to work first, and then look into why on() is better, and how to do it.

Assign a click function to your div that does:

$('#div_id').html(parseInt($('#div_id').html())++)

or something along those lines.

http://jsfiddle/4eqve/32/

  • Use a closure to store a counter variable for each DIV.
  • Attach a click handler.

Change counter function to this:

function counter(id){
   update( $('#'+id+">span") );
}

http://jsfiddle/4eqve/24/

demo stores each count in data. One big thing that would have helped you is apply a mon class to the elements you want to bind handler too as you'll see in this demo with added class "box"

If you're using jQuery then you might as well use the click handler it provides. You were quite close with your implementation, but you need to make sure that you're referencing the correct elements. I changed it so that you are passing the box div to the update function, that then selects the correct span from inside that div element.

// jQuery onclick for the boxes
$('#box1, #box2, #box3').click(function() {
   update(this);
});

function update(j) {
    var span = $(j).children('span');
    var n = parseInt(span.text())+1;
    span.text(n);
}

Here's the jsFiddle: http://jsfiddle/4eqve/29/ ​

Fixed that for you.

It is much cleaner when you have clean html, and seperate the javascript to do the work.

<body>
    <content>
        <div id="box1" class="count-div">A : <span class="num">0</span></div>
        <div id="box2" class="count-div">B: <span class="num">0</span></div>
        <div id="box3" class="count-div">C: <span class="num">0</span></div>
    </content>
</body>​

$(function(){
    $(".count-div").click(function(){
        amount = 1;
        value = parseInt($(this).find("span").html());
        $(this).find("span").html(value+amount);                
    });
});

You can even clean that up more so you have less code. If you have any question ask me

http://jsfiddle/4eqve/33/

There are many bugs in your script. Not to mentione, the markup selection is quite vague.

With a little update to some mark-ups, we can do this with a tiny snippet.

$(".clicable").click(function() {
    $(this).children("span").html(parseInt($(this).children("span").html()) + 1);
});

Check the demo here

本文标签: javascriptEach div must increments its counter upon clickingStack Overflow