jQuery - cannot bind events to dynamic elements?

I've come to maintain a piece of javascript that downloads some JSON data from the server, builds a new table row (like `$(''); and later, an event is bound to it like this: a.click(function () { // yadda yadda return false; }); The only problem is that this doesn't seem to work. Neither does binding through `on()` or the deprecated `live()`. The handler is simply "ignored", never fires and the page scrolls to the top (due to the `href="#"`). When binding the event, the element is already `append`ed to DOM. Any help would be greatly appreciated. Some contextual information that comes to mind: the element is created inside a loop iterating over the data, but that shouldn't be a problem unless javascript has some *really* weird stuff going on with scoping, plus *everything else* I try with the element works: I can change its content, styling, only the event binding doesn't work. And of course, the jQuery version, which is **1.8.3**.
Some more context would be good. Your code doesn't seem to be wrong.

以上就是jQuery - cannot bind events to dynamic elements?的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » jQuery 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准