Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/414.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 按键不处理动态创建的元素_Javascript_Jquery - Fatal编程技术网

Javascript 按键不处理动态创建的元素

Javascript 按键不处理动态创建的元素,javascript,jquery,Javascript,Jquery,我正在尝试在tags div中添加一个新的标记范围。 我的问题是span[id=“newtag”]下的代码不存在 工作。我怎样才能使这个代码工作 $(document).ready(function () { $('#tags').on("click", function () { $(this).append('<span id="newtag" contenteditable="true"></span>'); $(this).

我正在尝试在tags div中添加一个新的标记范围。 我的问题是
span[id=“newtag”]
下的代码不存在 工作。我怎样才能使这个代码工作

$(document).ready(function () {
    $('#tags').on("click", function () {
        $(this).append('<span id="newtag" contenteditable="true"></span>');
        $(this).children(":last").focus();
    });
    $('span[id="newtag"]').keypress(function (k) {
        if (k.which == 13 || k.which == 32) {
            $(this).append('gfskjsokdfla');
        }
    });
});
$(文档).ready(函数(){
$('#标记')。在(“单击”,函数(){
$(此)。附加(“”);
$(this.children(“:last”).focus();
});
$('span[id=“newtag”]”)。按键(函数(k){
if(k.which==13 | | k.which==32){
$(this.append('gfskjsokdfla');
}
});
});

对创建的动态dom元素使用
事件委派

$('#tags').on('keypress', 'span[id="newtag"]', function(k){
   if (k.which == 13 || k.which == 32) {
       $(this).append('gfskjsokdfla');
   }
});