.each()在IE中不起作用

时间:2010-06-21 04:01:19

标签: jquery-selectors

我想在div中获取所有标签,这段代码可以在Firefox中运行,而不是在IE中运行。任何的想法。提前谢谢。

<div id='discounts'>
  <label id="discount1"> discount 1</label>
  <label id="discount2"> discount 2 </label>
  <input type="text" id="discountmisc"  value="" />    
</div>

var selectLabels = {

    getLabels: function() {
        $('#discounts > label').each(function(index, item) {
            alert(index + $(item).attr('id'));
        });
    }
};

selectLabels.getLabels();

1 个答案:

答案 0 :(得分:2)

你是否包含DOM Ready功能?即。

$(function () {
    var selectLabels = {
        getLabels: function() {
            $('#discounts > label').each(function(index, item) {
                alert(index + $(item).attr('id'));
            });
        }
    };

    selectLabels.getLabels();
});

或者:

var selectLabels = {
    getLabels: function() {
        $('#discounts > label').each(function(index, item) {
            alert(index + $(item).attr('id'));
        });
    }
};

$(selectLabels.getLabels);

或者最后(因为你不关心返回值):

var selectLabels = {
    getLabels: function() {
        $(function () {
            $('#discounts > label').each(function(index, item) {
                alert(index + $(item).attr('id'));
            });
        });
    }
};

selectLabels.getLabels();

告诉我,如果是的话,我会改变我的答案。