[英]How to properly use jquery prev?
How to select the previous element using prev() ? 如何使用prev()选择前一个元素? I want when I click
icon 1
it removes the span
. 我想在单击
icon 1
时删除span
。
html.erb html.erb
# inside a loop -- notice the loop!.
<span data-id="foo_span">
<i data-id="bar">icon 1</i>
<i>icon 2</i>
</span>
JavaScript: JavaScript:
$("[data-id=bar]").click(function(e){
e.preventDefault();
//$(this).prev("[data-id=foo_span]").remove();
$(this).prev().remove();
});
Am I using prev()
correctly? 我是否正确使用
prev()
? Based on the examples, it uses .css
. 根据示例,它使用
.css
。 What's the correct method to remove the span tag? 删除span标签的正确方法是什么?
i
must be inside a span tag. i
必须在span标签内。
This seems to work for me. 这似乎为我工作。 Is this what you want?
这是你想要的吗?
https://jsfiddle.net/ndva0rkz/ https://jsfiddle.net/ndva0rkz/
$("body").on("click", ".f", function(e){
e.preventDefault();
//$(this).prev("[data-id=foo_span]").remove();
$(this).prev().remove();
});
<span data-id="foo_span">
<i class="f" data-id="bar">icon 1</i>
<i class="f">icon 2</icon>
</span>
What about using parent() ? 那使用parent()呢?
$("[data-id=bar]").click(function(e){
e.preventDefault();
$(this).parent().remove();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.