Add styles to dynamically added elements with jQuery

江枫思渺然 提交于 2019-12-25 01:42:33

问题


heres the demo

http://lufi.realservers.info/demos/add_forms_dynamically/

when i add another element (input boxes) i cannot remove the italicized-grayed labels inside unlike the first input boxes. Also, they only disappear if I click the first set of input boxes.

are there any other ways to add styles to dynamically added elements?


回答1:


You can try the live() method to apply it to dynamically added element.




回答2:


As well as using live for your events (which will apply to things not yet in the dom but will add events when they get added) you might also want to change your function that remove the text from the input because at the moment it will just remove all, which is probably not what you are wanting.

Instead of:

$('.inbox').css('color','black');
$('.inbox').css('font-style','normal');
$('.inbox').val('');

Try

$(this).css({'color': 'black', 'font-style': 'normal'}).val('');



回答3:


you can also go for jQuery.delegate()



来源:https://stackoverflow.com/questions/6505218/add-styles-to-dynamically-added-elements-with-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!