.remove()在IE和Firefox中不起作用 [英] .remove() not working in IE and Firefox

查看:103
本文介绍了.remove()在IE和Firefox中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过jQuery .load()方法加载div元素,如下所示:

I'm loading div element through jQuery .load() method like this:

$(document).ready(function() {
    $(".module-wrapper").load("index.php?option=com_k2&view=itemlist&Itemid=974 .genericItemList");
});

通过我得到的项目列表,我们说其中的5个.

Through that I get list of items, let's say 5 of them.

<div class="module-wrapper">
    <div class="genericItemList">
        <div class="genericItemView">Item 1</div>
        <div class="genericItemView">Item 2</div>
        <div class="genericItemView">Item 3</div>
        <div class="genericItemView">Item 4</div>
        <div class="genericItemView">Item 5</div>
    </div>    
</div>

现在,我想使用jQuery .remove(),因为我只想显示前3个项目. 上面的HTML只是示例,实际上每个项目都有很多HTML代码,因此我想使用jQuery .remove()而不是CSS display:none.

Now I want to use jQuery .remove() because I want to show just first 3 items. HTML above is just example, in reality each item has a lot of HTML code so I want to use jQuery .remove() instead of CSS display:none.

我这样做是这样的:

$(window).load(function() {
    $(".module-wrapper .genericItemView:gt(2)").remove();
});

这仅适用于Chrome,但不适用于Firefox或IE,我可以在其中看到所有5个项目.

This is working only Chrome, but not in Firefox or IE, where I can see all 5 items.

有什么建议吗?

推荐答案

为确保代码仅在元素加载后才运行,应将其放在传递给

To ensure that code only runs after the elements have been loaded, you should put it in the callback function passed to load():

$(document).ready(function() {
    $(".module-wrapper").load(
        "index.php?option=com_k2&view=itemlist&Itemid=974 .genericItemList",
        function() {
            $(".module-wrapper .genericItemList:gt(2)").remove();
        }
    );
});

您的类选择器也可能是错误的,我试图在上面的代码中对其进行更正.看起来您想匹配显示genericItemList类的.module-wrapper的后代,但是您原来的选择器匹配显示了module-wrappergenericItemView类的元素.

Your class selector may also be wrong, I tried to rectify it in the code above. It looks like you want to match descendants of .module-wrapper that expose the genericItemList class, but your original selector matches the elements that expose both the module-wrapper and genericItemView classes instead.

这篇关于.remove()在IE和Firefox中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆