如何使用javascript删除以前的元素

时间:2016-05-09 15:30:40

标签: javascript jquery

<script type="text/javascript">
    $(document).ready(function(){
        $('.linkplus').next().remove();
    });
</script>

这很有效。但我不明白为什么

<script type="text/javascript">
    $(document).ready(function(){
        $('.linkplus').prev().remove();
    });
</script>

<script type="text/javascript">
    $(document).ready(function(){
        $('.linkplus').prev().remove();
    });
</script>

不行。有没有办法使用javascript删除前一个元素?

前一个元素是:

<%= link_to '+', food_plans_path(abc: date),:method=> :post,:style=>"color:black;visibility:visible;float:right;font-weight:700;font-size:17px;",:class=>"linkplus"%>

我希望仅在linkplus

之前删除linkx

这是HTML:

<div class="col-md-9" style="padding-top:7px;">
<%= week_calendar number_of_weeks: 4, events: @food_plans do |date, food_plans| %>
  <%= date.day %>
  <%= link_to '+', food_plans_path(abc: date),:method=> :post,:style=>"color:black;visibility:visible;float:right;font-weight:700;font-size:17px;",:class=>"linkplus"%>
  <% food_plans.each do |food_plan| %>
  <%if food_plan!=nil%>
  <%= link_to 'x', food_plan,:style=>"color:black;visibility:visible;float:right;font-weight:700;font-size:14px;",:class=>"linkx", method: :delete, data: { confirm: 'Are you sure?' } %>
  <%end%>
  <div >
  <!-- Displays proper collection select-->
  <%if food_plan.meal==nil%>
  <%=collection_select(:post, :food_plan_id, Meal.all, :name, :name, {prompt: '---'}, {:id=>"fsel", :onchange=>"window.location='#{meal_select_path(food_plan.id)}?meal=' + this.value"})%>
  <%else%>
  <%=collection_select(:post, :food_plan_id, Meal.all, :name, :name, {selected: food_plan.meal}, {:id=>"fsel", :onchange=>"window.location='#{meal_select_path(food_plan.id)}?meal=' + this.value"})%>
  <%end%>

  </div>
  <%end%>
<% end %>
</div>

在inspect-ff上显示:

<a style="color:black;visibility:visible;float:right;font-weight:700;font-size:17px;" class="linkplus" rel="nofollow" data-method="post" href="/food_plans?abc=2016-05-09">+</a>
<a style="color:black;visibility:visible;float:right;font-weight:700;font-size:14px;" class="linkx" data-confirm="Are you sure?" rel="nofollow" data-method="delete" href="/food_plans/7">x</a>

1 个答案:

答案 0 :(得分:0)

  

我希望仅在linkplus

之前删除linkx

您显示的代码会删除linkplus元素之前或之后的元素,而不是删除元素本身。

这是如何在上面引用的陈述中做出的要求:

$('.linkx').prev('.linkplus').remove();