我正在使用 Ajax/Request 将元素加载到 div 容器中。默认情况下我隐藏一个输入框。如果用户单击该 div 上的编辑图标,我想显示输入框。这是我的代码:
HTML代码
<div class='container'>
<input type = 'text' onkeydown='saveFn(event,this)' name = 'editemail' class = 'editemail' style='display:none; height:20px;' />
</div>
JS CODE
$(".container").click(function(){
console.log($(this).find(".editemail").show()); //Note it works fine If i didn't load any new elements into the div.
});
将新元素加载到容器之前的控制台日志输出。
<input type="text" onkeydown="saveFn(event,this)" name="editemail" class="editemail" style="height: 20px; " value="hyth[email protected] /cdn-cgi/l/email-protection" title="press enter to save">
将元素加载到容器中后的控制台日志输出。
<input type="text" onkeydown="saveFn(event,this)" name="editemail" class="editemail" style="height: 20px; display: none; " value="saravan[email protected] /cdn-cgi/l/email-protection" title="press enter to save">
尽管我也尝试从此元素中删除“style”属性并添加新的样式元素,但它仍然不起作用。
首先您应该阅读 jQuery 文档常见问题解答部分:Why_do_my_events_stop_working_after_an_AJAX_request http://docs.jquery.com/Frequently_Asked_Questions#Why_do_my_events_stop_working_after_an_AJAX_request.3F
使用 on() 委托未来元素的处理程序
$(document).on('click', ".container", function(){
/* your code*/
})
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)