IE7无法根据它的href属性删除元素

时间:2010-10-07 01:02:04

标签: jquery internet-explorer-7 internet-explorer-6

啊,如果还有IE,它真的结束了吗?

感谢Stack Overflowers的宝贵帮助,我让我的jQuery在FF,Safari,Chrome&奥普拉。当然,它在IE7中失败,显然它有很多href属性,例如:

$('li a[href="' + title + '"]').parent().remove();

有人可以了解一下IE7会理解的替代语法,以便从列表中删除未经检查的项目吗?非常感谢提前!

以下是作品:

<div class="product-module">
<div class="product-pic">
    <div class="checkbox-wrapper">
        <label for="compare1">
            <input type="checkbox" class="checkbox" name="compare" id="compare1" />
            Compare
        </label>
    </div>
</div>

<div class="product-info">
    <p><a href="#" title="#"><span class="product-name">Product Name here</span></a></p>    
</div>

<div class="compare">
<ul>
</ul>
<p class="compare-button"><button type="submit">Compare</button></p>
<p class="clear-selections"><a class="button" id="clear-selections" href="#">Clear Selections</a></p>

<script type="text/javascript">

// clear all checkboxes on load
$(function(){
    $('input[type="checkbox"]').attr('checked', false);
});


$(function(){
    $('.column-main input[type="checkbox"]').click(function()    {
        var title = $(this).closest('.product-module').find('.product-name').html();

        // if user checks the checkbox, add the item to the ul
        if    ($(this).attr('checked'))    {
        var html = '<li><a href="'+title+'">' + title + '</a></li>';
        $('.compare ul').append(html);

        // un-checking the checkbox removes the corresponding item from the ul
        }    else {
        // $('.compare li a').attr('href', title).parent().remove();
        $('li a[href="' + title + '"]').parent().remove();        //    works in real browsers; fails in IE7
        }
    })
});



    $(function(){
    $('.clear-selections').click(function(){
        $('.compare ul').empty();
        $('input[type="checkbox"]').attr('checked', false);
    })
});


$(function(){
    $('.compare button').click(function(){
        minRequests = 2;
        maxRequests = 3;
        requested = $('.compare ul li').size();    // go figure: why not .length()?

        if(requested < 2)    {
        alert ('Compare ' + requested + ' products?');

        } else if((requested >= 2) && (requested <= 5 ))    {
        alert ('There are ' + requested + ' products to compare');

        } else {
        alert (requested + ' is too many');
        }
    });
});

1 个答案:

答案 0 :(得分:0)

您是否将元素的titlehref进行了比较?我相信在IE中它使用绝对URL而不是相对,所以你可能想要在假设某些东西有用之前仔细检查href值。

您可以使用[href*=代替href=

永远不要假设,请务必检查。在attr('href')上发出提醒,并确保它们匹配。