swetlana Posted April 21, 2011 Report Share Posted April 21, 2011 Есть кнопка. По нажатию на эту кнопку создаётся элемент:$(document).ready(function(){ $("#a_link").click(function(){ $("body").append('<div id="a_out" />');…}); });Далее нужно, чтобы по клику на этот элемент выполнялась следующая функция.Если написать:$(document).ready(function(){ $("#a_out").click(function(){…}); });то не сработает ибо на момент чтения браузером этого скрипта такого элемента не существует.А как сделать правильно? Quote Link to comment Share on other sites More sharing options...
0 psywalker Posted April 21, 2011 Report Share Posted April 21, 2011 Какой нибудь window.onload, не? Quote Link to comment Share on other sites More sharing options...
0 swetlana Posted April 21, 2011 Author Report Share Posted April 21, 2011 новый элемент создаётся значительно позже onload Quote Link to comment Share on other sites More sharing options...
0 s0rr0w Posted April 21, 2011 Report Share Posted April 21, 2011 А как сделать правильно?Ох уж эти джекверисты... Пучеглазы, мускулисты...По идее так должно работать$(document).ready(function(){ $("#a_link").click(function(){ $("body").append('<div id="a_out" />').click(function(){ … }); …}); }); 2 Quote Link to comment Share on other sites More sharing options...
0 Doka Posted April 21, 2011 Report Share Posted April 21, 2011 Ох уж эти джекверисты... Пучеглазы, мускулисты...По идее так должно работать$(document).ready(function(){ $("#a_link").click(function(){ $("body").append('<div id="a_out" />').click(function(){ … }); …}); });А точнее $(function(){ $("#a_link").click(function(){ $("body").append('<div id="a_out" />').live('click',function(){ }); });}надо использовать live т.к DOM узел создается динамически. jquery.live 2 Quote Link to comment Share on other sites More sharing options...
0 swetlana Posted April 21, 2011 Author Report Share Posted April 21, 2011 Doka, спасибо.Именно то, что нужно. Quote Link to comment Share on other sites More sharing options...
0 swetlana Posted April 21, 2011 Author Report Share Posted April 21, 2011 а точнее, почти то, что нужно.Правильнее так:$(function(){ $("#a_link").click(function(){ $("body").append('<div id="a_out" />'); $("#a_out").live('click',function(){… }); });}а то, если $("body").append().live(), то обработчик на body и вешается. Quote Link to comment Share on other sites More sharing options...
0 iandsociety Posted April 21, 2011 Report Share Posted April 21, 2011 (edited) $(function(){ $("#a_link").click(function(){ $('<div id="a_out" />').click(function(){ … }).appendTo('body'); });});Вот Edited April 21, 2011 by iandsociety 1 Quote Link to comment Share on other sites More sharing options...
0 Doka Posted April 22, 2011 Report Share Posted April 22, 2011 а точнее, почти то, что нужно.Правильнее так:$(function(){ $("#a_link").click(function(){ $("body").append('<div id="a_out" />'); $("#a_out").live('click',function(){… }); });}а то, если $("body").append().live(), то обработчик на body и вешается.Аа! Ну да, извиняюсь - пропустил ошибочку)) 1 Quote Link to comment Share on other sites More sharing options...
Question
swetlana
Есть кнопка. По нажатию на эту кнопку создаётся элемент:
Далее нужно, чтобы по клику на этот элемент выполнялась следующая функция.
Если написать:
то не сработает ибо на момент чтения браузером этого скрипта такого элемента не существует.
А как сделать правильно?
Link to comment
Share on other sites
8 answers to this question
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.