[英]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();
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.