Jquery - 无法选择父div

时间:2008-12-18 19:14:32

标签: jquery dom

我希望这不是浪费时间,但我真的一直试图解决这个问题。这是我的语法。我只想在点击一个“.remove-link”类的链接后删除父div“.number-row”。

提前致谢

<script>
$(document).ready(function(){
    $(".remove-link").click(function() {
        $(this).parent(".number-row").hide();
    })
})
</script>

<div class="number-row" >
   <div class="number-column">
         <div class="label-row">
             Select Country:
         </div>
         <div class="input-row">
             <select>
                 <option>1</option>
                 <option>2</option>
             </select>
         </div>
         <div class="label-row">
             Select Toll Free or City:
         </div>
         <div class="input-row">
             <select>
                 <option>1</option>
                 <option>2</option>
             </select>
         </div>
         <div class="label-row">
             Select Your Number:
         </div>
         <div class="input-row">
             <select>
                 <option>1</option>
                 <option>2</option>
             </select>
         </div>
     </div>
   <div class="number-column">
         <div class="label-row">
             Select Country:
         </div>
         <div class="input-row">
             <select>
                 <option>1</option>
                 <option>2</option>
             </select>
         </div>
         <div class="label-row">
             Enter Number to Forward to:
         </div>
         <div class="input-row">
            <input type="text" name="forward_number" id="forward_number" />
         </div>
         <div class="number-row-options">
            <a class="save-link" href="#">Save</a>
            <a class="remove-link" href="#">Remove</a>
         </div>                    
     </div>                    
</div>

2 个答案:

答案 0 :(得分:7)

尝试使用parents()代替parent()

$(document).ready(function(){
    $(".remove-link").click(function() {
        $(this).parents(".number-row").eq(0).hide();
    })
})

答案 1 :(得分:2)

应该这样做......

$(document).ready(function(){ 
    $(".remove-link").click(function() { 
        $(this).parent().parent().parent().hide();
    }) 
})

请注意,这不是您要求的删除;它只是隐藏它。您可以使用remove()代替hide()将其从DOM中删除。