请帮我解决这个问题。我做了更多的研究,但我不能解决问题。我有代码1:
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>
<script>
function needloadafterloadpage(){
$form = $('<form method="get" action="" id="form-cmtxxx"></form>');
$form.html('<textarea name="add_comment_text" title="Write a comment..." content="Write a comment..." placeholder="Write a comment..." class="textInput mentionsTextarea uiTextareaAutogrow uiTextareaNoResize UFIAddCommentInput DOMControl_placeholder" id="scriptBox26" />');
$('#wrappercontentpost').html($form);
}
</script>
<body>
<div id="wrappercontentpost"></div>
<script>
$('textarea').on('keydown',function(e){
alert("abc");
});
</script>
<script>
needloadafterloadpage();
</script>
</body>代码jquery在这种情况下不起作用。我想在10s之后将form附加到div。你有没有办法重新加载Jquery。非常感谢您的支持
发布于 2013-10-05 23:33:18
您需要使用事件委托,因为在执行事件注册脚本时,dom中不存在textarea元素。
然后使用setTimeout在10秒后追加表单
所以总体而言
function needloadafterloadpage() {
$form = $('<form method="get" action="" id="form-cmtxxx"></form>');
$form.append('<textarea name="add_comment_text" title="Write a comment..." content="Write a comment..." placeholder="Write a comment..." class="textInput mentionsTextarea uiTextareaAutogrow uiTextareaNoResize UFIAddCommentInput DOMControl_placeholder" id="scriptBox26" />');
$('#wrappercontentpost').append($form);
}
jQuery(function ($) {
$('#wrappercontentpost').on('keydown', 'textarea', function (e) {
console.log("abc");
});
})
setTimeout(needloadafterloadpage, 10000)演示:Fiddle
发布于 2013-10-05 23:35:43
更改代码的一部分:
<script>
$('textarea').on('keydown',function(e){
alert("abc");
});
</script>只有一个:
<script>
$(function() {
$('textarea').on('keydown',function(e){
alert("abc");
});
});
</script>说明:您没有使用$(function(){ ... });初始化jquery库
发布于 2013-10-05 23:38:36
$(document).ready(function() {
var form = '<form method="get" action="" id="form-cmtxxx"><textarea name="add_comment_text" title="Write a comment..." content="Write a comment..." placeholder="Write a comment..." class="textInput mentionsTextarea uiTextareaAutogrow uiTextareaNoResize UFIAddCommentInput DOMControl_placeholder" id="scriptBox26" /></form>';
window.setTimeout(function(){$('#wrappercontentpost').html(form);}, 10000);
});将使表单按要求在10秒后出现。
https://stackoverflow.com/questions/19199409
复制相似问题