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

javascript - jQuery toggle dynamically created elements - Stack Overflow

programmeradmin5浏览0评论

I've got something like this, but it doesn't work:

<!-- ... code .. -->

var id = 0;        

data.forEach(function(item, i) {
      id = id+1;

$( ".button-" + id ).click(function() {
$( ".button-" + id ).toggle();

});

<!-- ... code .. -->

What I want: There are some DIVs like button-1, button-2 ... and so on. I made an toggle and I wanted to automate it with this loop, because the content is dynamically generated. But this seems to be the wrong way... In this loop it does not work.

So how to make toggles dynamically for button-1, button-2, ... ??

It's just about this, which seems to be wrong:

$( ".button-" + id ).click(function() {
$( ".button-" + id ).toggle();

EDIT

Well, maybe my question was a little bit messy

发布评论

评论列表(0)

  1. 暂无评论