简体   繁体   English

我该如何移除 <p> 在Rails 4中使用jQuery标签

[英]how do i remove <p> tag using jquery in rails 4

I have: 我有:

$(document).ready(function() {
    $("#btn1").click(function() {
        $("#po").append("<p><%= j f.datetime_select :dtmRealshow%><a>Remove</a></p>");
    });   
});


<p id="po"></p>
<button id="btn1">+</button>

When i click on the btn1 a new datetime_select field is added to the form.But i want to remove the parent <p> tag after clicking on the remove link.Please help me out. 当我点击btn1 ,新的datetime_select字段被添加到表单中。但是我想在点击删除链接后删除父<p>标签。请帮帮我。

Add a css class to anchor 将CSS类添加到锚点

$("#po").append("<p><%= j f.datetime_select :dtmRealshow%><a class='remove'>Remove</a></p>");  });

Then you can use Event Delegation using .on() delegated-events approach to bind click handler to anchor . 然后,您可以使用事件委托,使用.on()委托事件方法将点击处理程序绑定到anchor

$("#po").on('click', ".remove", function(){
    $(this).closest('p').remove();
});

Removing the parent <p> will remove it's children along with it. 删除父<p>将同时删除其children

If that's what you're looking for then try the following code. 如果这是您要查找的内容,请尝试以下代码。

$(document).ready(function(){
    $("#btn1").click(function(){
        $("#po").append("<p><a id='remove'>Remove</a></p>");
        $("#remove").click(function() {
            $(this).closest('p').remove();
        });
    });
});

Here is the demo 这是演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM