Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.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 无法将事件附加到动态创建的html_Javascript_Jquery - Fatal编程技术网

Javascript 无法将事件附加到动态创建的html

Javascript 无法将事件附加到动态创建的html,javascript,jquery,Javascript,Jquery,我想将事件分配给动态创建的元素,但它不起作用。 我的代码是: $("#dvInvitar").on("click", ".spDelMail", function () { alert($this.html()); $(this).remove(); }); $("#email072").bind('keypress', function (e) { var code = e.keyCode ? e.keyCode : e.which; //var id = $(

我想将事件分配给动态创建的元素,但它不起作用。 我的代码是:

$("#dvInvitar").on("click", ".spDelMail", function () {
    alert($this.html());
    $(this).remove();
});
$("#email072").bind('keypress', function (e) {
    var code = e.keyCode ? e.keyCode : e.which;
    //var id = $(this).attr("id070");
    if (code == 13) // Enter key is pressed
    {
        e.preventDefault();
        if (esMailValido($(this).val())) {
            $("#dvInvitar ul").prepend(
                '<li style="overflow: hidden;height: auto !important;height: 15px;margin: 3px;padding: 1px 3px;background-color: #eff2f7;color: #000;cursor: default;border: 1px solid #ccd5e4;font-size: 11px;border-radius: 5px;-moz-border-radius: 5px;-webkit-border-radius: 5px;float: left;white-space: nowrap;">' + '<p style="display: inline;padding: 0;margin: 0;">' + $(this).val() + '</p>' + '<span class="spDelMail" style="color: #a6b3cf;margin-left: 5px;font-weight: bold;cursor: pointer;">x</span>' + '</li>');
            $(this).val("");
        } else alert("<?=_("
        Tenés que ingresar una dirección de correo correcta ")?>");
    }
});
$(“#dvInvitar”)。在(“单击“,”.spDelMail”,函数(){
警报($this.html());
$(this.remove();
});
$(“#email072”).bind('keypress',函数(e){
var代码=e.keyCode?e.keyCode:e.which;
//var id=$(this.attr(“id070”);
如果(代码==13)//按下回车键
{
e、 预防默认值();
if(esMailValido($(this.val())){
$(“#dvInvitar ul”)。前置(
“
  • ”+”

    “+$(this.val()+”

    “+”x“+”
  • ”; $(此).val(“”); }else警报(“”); } });
    HTML是

    <div id="dvInvitar" style="; font-size: 0.8em">
        <ul style="overflow: hidden;height: auto !important;height: 1%;width: 95%;border: 1px solid #8496ba;cursor: text;font-size: 12px;font-family: Verdana;min-height: 1px;z-index: 999;margin: 0;padding: 0;background-color: #fff;list-style-type: none;clear: left;">
            <li style="float: left;margin: 0;padding: 0;list-style-type: none;">
                <input id="email072" type="text" style="outline: none;width: 30px;border: 0;width: 100px;padding: 3px 8px;background-color: white;margin: 2px 0;-webkit-appearance: caret;" />
            </li>
        </ul>
    </div>
    
    
    

    您需要使用委托选择器,因为您在
    .spDelMail
    元素上。试试这个:

    $(document).on('keypress', "#email072", function(e) {
        // rest of your code...
    }
    

    它起作用了。第一行有一个错误,它说$this,需要说$(this)

    OP已经在使用事件委派<代码>$(“#dvInvitar”)。在(“单击“,”.spDelMail”,函数()上{@blueskie不是因为
    #email072
    元素,他不是。那又怎样?那里不需要它。既然OP是动态地附加那个元素,为什么你会认为不需要委托选择器呢?因为没有理由相信那个元素被附加。问题表明,作为现有的HTML。附加的元素在
    keypress
    处理程序中。既然没有问题,就删除你的问题。什么不起作用。有错误消息吗?