如何使用我的Jquery代码创建委托事件侦听器

时间:2021-04-07 20:45:06

I am trying to make a delegated event listener so that when the user clicks one time the p tag is removed and an input box is appended. On a second click the input box is removed and and the p tag is appended.

我正在尝试创建一个委托事件监听器,以便当用户单击一次时删除p标记并附加一个输入框。在第二次单击时,将删除输入框,并附加p标记。

Here is my code. Right now nothing works at all. Thank you!

这是我的代码。现在什么都没有用。谢谢!

    //User clicks p tag named "#hour1". 
$(document).ready(function(){

    $("document").on('click', "#hour1", function( event ){
        $("#hour1").remove();
        $("#hour1Data").append("<input id='hour1Input' type='text'/>");
    });
    });

//On Second click, input box is sent back to data box.
$(document).ready(function(){
    $("document").on("click", "#hour1Input", function( event ){
        $("#hour1Input").remove();
        $("#hour1Data").append("<p id='hour1'><?php echo $hour1; ?></p>");
    });
});

1 个解决方案

#1


0  

You are passing a string containing a selector to jQuery and trying to bind the event handler to all <document> elements in the document. This fails because you don't have any (because HTML doesn't have any).

您正在将包含选择器的字符串传递给jQuery,并尝试将事件处理程序绑定到文档中的所有 元素。这失败是因为你没有(因为HTML没有)。

Pass the document object instead of the "document" selector.

传递文档对象而不是“文档”选择器。

#1


0  

You are passing a string containing a selector to jQuery and trying to bind the event handler to all <document> elements in the document. This fails because you don't have any (because HTML doesn't have any).

您正在将包含选择器的字符串传递给jQuery,并尝试将事件处理程序绑定到文档中的所有 元素。这失败是因为你没有(因为HTML没有)。

Pass the document object instead of the "document" selector.

传递文档对象而不是“文档”选择器。