jquery 将 li 添加到 ul 末尾,但最后创建的 li 未被识别为最后一个

发布于 2024-08-22 07:31:11 字数 1517 浏览 1 评论 0原文

我有以下代码。单击最后一个 li 上的按钮后,我可以创建新的 li。但是,一旦创建,如果我单击创建的最新按钮,则会创建新的 li。看来它没有将最后创建的 li 识别为最后一个 li。有人可以帮忙解决这个问题吗?

<html xmlns="http://www.w3.org/1999/xhtml" >

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $("#mylist :button").click(function() {
                    var text = $(this).val();
                    if (text == "Save") {
                    }
                });

                $("#mylist li:last-child").click(function() {
                $(this).parent().append('<li>' + '<input type = "textbox">' + '<input type = "button" value= "Save">' + '</li>');

                });

            });

        </script>

<div>
<ul id="mylist">
<li id="1">1<button id="Button3">Delete</button> </li>    
<li id="2">2<button id="Button2">Delete</button></li>
<li id="3">3<button id="another_entry">Save</button></li> 
</ul>

<ul id="mylist2">
<li> test1 <button id="Button6">Delete</button></li>
<li> test2<button id="Button5">Delete</button> </li>
<li id="6"><button id="Button1">Delete</button></li> 
</ul>


</div>

I have the following code. On cliking the button on the last li, I can create the new li. However, once created, if I click on the latest button created, new li is created. It seems it doesn`t recognise the last created li as the last li. Can someone help on this please.

<html xmlns="http://www.w3.org/1999/xhtml" >

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $("#mylist :button").click(function() {
                    var text = $(this).val();
                    if (text == "Save") {
                    }
                });

                $("#mylist li:last-child").click(function() {
                $(this).parent().append('<li>' + '<input type = "textbox">' + '<input type = "button" value= "Save">' + '</li>');

                });

            });

        </script>

<div>
<ul id="mylist">
<li id="1">1<button id="Button3">Delete</button> </li>    
<li id="2">2<button id="Button2">Delete</button></li>
<li id="3">3<button id="another_entry">Save</button></li> 
</ul>

<ul id="mylist2">
<li> test1 <button id="Button6">Delete</button></li>
<li> test2<button id="Button5">Delete</button> </li>
<li id="6"><button id="Button1">Delete</button></li> 
</ul>


</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

两仪 2024-08-29 07:31:11

您需要使用 live 而不是 bind,因为绑定仅迭代元素一次,但是 live 创建一个 侦听器,它动态附加事件,甚至附加到新创建的元素。

您的代码应符合此示例:

$("#mylist li:last-child")
    .live('click', function() {
        // append element
    });

使用 jQuery 1.4.0 进行测试

You need to use live instead of bind as bind iterates elements only once, however live creates a listener, which attaches events dynamically, even to newly created elements.

You code should conform to this sample:

$("#mylist li:last-child")
    .live('click', function() {
        // append element
    });

tested with jQuery 1.4.0

寻梦旅人 2024-08-29 07:31:11

而不是:

 $(this).parent().append('<li>'.........

尝试

 $("#mylist").append('<li>'.........

Instead of:

 $(this).parent().append('<li>'.........

Try

 $("#mylist").append('<li>'.........
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文