0

请帮我解决这个问题。我研究更多,但我无法解决问题。我有代码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 不起作用。我想在 10 秒后将表单附加到 div 。你有什么方法可以重新加载 Jquery。非常感谢您的支持

4

4 回答 4

1

您需要使用事件委托,因为在执行事件注册脚本时,dom 中不存在 textarea 元素

然后使用setTimeout10 秒后追加表单

所以总的来说

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)

演示:小提琴

于 2013-10-05T15:33:18.280 回答
0
$(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 秒后显示表单。

于 2013-10-05T15:38:35.817 回答
0

尝试这个:

<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>
<script>
function addForm() {
    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>';
    $('#wrappercontentpost').html(form);
}

$(document).ready(function() {
    setTimeout(function() {
        addForm();
    },10000);
});
</script>
<body>
<div id="wrappercontentpost"></div>
</body>

演示:http: //jsfiddle.net/WrBrx/

于 2013-10-05T15:39:03.300 回答
0

更改部分代码:

<script>    
    $('textarea').on('keydown',function(e){
        alert("abc");
    });
</script>

有一个:

<script>    
    $(function() {
       $('textarea').on('keydown',function(e){
          alert("abc");
       });
   });
</script>

说明:您没有使用初始化 jquery 库$(function(){ ... });

于 2013-10-05T15:35:43.380 回答