开发者

jQuery .size() question

开发者 https://www.devze.com 2023-02-06 01:24 出处:网络
When you click the \"Add Div\" button, why doesn\'t the number incr开发者_开发问答ement? http://jsfiddle.net/cN2zR/size() is being evaluated once, when the value is assigned to d. You\'ll need to mov

When you click the "Add Div" button, why doesn't the number incr开发者_开发问答ement?

http://jsfiddle.net/cN2zR/


size() is being evaluated once, when the value is assigned to d. You'll need to move the assignment inside the function to evaluate it each time.

Or, if you want it to keep it outside, you can make it into a function:

var d = function() { return '<div class="historyItem">Test ' + ($("#history > div").size() + 1) + '</div>'; }

then

$('#history').append(d());


http://jsfiddle.net/cN2zR/3/ It does now :)

make d a function so it can be run each add.

var d = function(){
   return '<div class="historyItem">Test ' + ($("#history > div").size() + 1) + '</div>';
}

Also I noticed your problem with the scroll link. A quick trick to scroll to the bottom of a container is using the .scrollTop([arbitrary high number])


Because the size of the jquery result is calculated inline and ont everytime you use . You can instead change d to a function that returns the HTML string and use it so that you get the size calculated every time you refer to it.

d = function(){
    return '<div class="historyItem">Test ' + ($("#history > div").size() + 1) + '</div>';
}

$('#b').click(function() {
    //console.time("timing Time");
    $('#history').append(d());
    //console.timeEnd("timing Time");
    $('#history').scrollTo('max');
});

Check this Post: http://jsfiddle.net/cN2zR/7/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号