在使用.each()循环时获取数据属性将返回undefined

时间:2017-05-02 12:37:41

标签: javascript jquery

我正在使用一系列数据在我的网页上存储数据。

    <div style="display: none;">
       <span class="data_location" data-name="Joe Bloggs" data-description="Lorem ipsum" data-location="CA"></span>
       <span class="data_location" data-name="Jane Doe" data-description="Ipsom erum" data-location="AN"></span>
       <span class="data_location" data-name="John Doe" data-description="Dorem noloy" data-location="CZ"></span>
       <span class="data_location" data-name="William Gates" data-description="Lorem ipsum" data-location="AG"></span>
       <span class="data_location" data-name="Henry Kissinger" data-description="Nuymt calum" data-location="AN"></span>
    </div>

我想循环遍历每个跨度,读取我的数据属性。下面的概念证明代码总是返回&#34; undefined&#34;。

        $(document).ready(function () {
            $.each(".data_location", function () {
                var location = $(this).data("location")
                alert(location)
            });
        });

我觉得问题在于$(这个),但我无法识别。任何人都可以帮助我吗?

4 个答案:

答案 0 :(得分:4)

select t.ID, c.name, sum(t.amount) from table1 t left join customers c on table1.ID = customers.ID2 group by t.ID, c.name having sum(t.amount) between 1000 and 10000 函数被错误判断。你可以这样改变array#each

为什么未定义?

您正在应用每种类型的数组方法。但是$(".data_location").each(function () {不是数组。元素的对象

$(".data_location")
$(document).ready(function () {
            $(".data_location").each(function () {
                var location = $(this).data("location")
                console.log(location)
            });
        });

答案 1 :(得分:0)

您可以参考以下代码。

-- DO THIS FOR TICKET #3
UPDATE  records
SET     statue = True
    -- No need to flip it if it is already True
WHERE status <> True
    -- New condition for ticket #3
AND true_criteria IN ( 'something')
    -- regression ticket #1
AND this_criteria NOT IN( 'that' )
    -- regression ticket #2
AND another_criteria NOT IN ('whatever')
    ;
 $(document).ready(function () {
         $.each($(document).find(".data_location"), function () {
                var location = $(this).data("location");
                alert(location);
            });
            });

答案 2 :(得分:0)

您的每个函数都是正确的,但回调方法必须包含2个参数1作为索引,另一个参数包含元素。

$(document).ready(function () {
    $.each($(".data_location"), function (index, element) {
       // Your $(this) equivalent will be $(element)
       var location = $(element).data("location");
       console.log(location);
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div style="display: none;">
       <span class="data_location" data-name="Joe Bloggs" data-description="Lorem ipsum" data-location="CA"></span>
       <span class="data_location" data-name="Jane Doe" data-description="Ipsom erum" data-location="AN"></span>
       <span class="data_location" data-name="John Doe" data-description="Dorem noloy" data-location="CZ"></span>
       <span class="data_location" data-name="William Gates" data-description="Lorem ipsum" data-location="AG"></span>
       <span class="data_location" data-name="Henry Kissinger" data-description="Nuymt calum" data-location="AN"></span>
    </div>

答案 3 :(得分:0)

问题是因为$.each()逻辑this内没有引用迭代中的元素。如果要使用此模式,则需要使用传入处理函数的第二个参数,该函数是当前元素。试试这个:

$(document).ready(function() {
  $.each($(".data_location"), function(i, obj) {
    var location = $(obj).data("location")
    console.log(location)
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div style="display: none;">
  <span class="data_location" data-name="Joe Bloggs" data-description="Lorem ipsum" data-location="CA"></span>
  <span class="data_location" data-name="Jane Doe" data-description="Ipsom erum" data-location="AN"></span>
  <span class="data_location" data-name="John Doe" data-description="Dorem noloy" data-location="CZ"></span>
  <span class="data_location" data-name="William Gates" data-description="Lorem ipsum" data-location="AG"></span>
  <span class="data_location" data-name="Henry Kissinger" data-description="Nuymt calum" data-location="AN"></span>
</div>

然而,在这种情况下使用$('.data_location').each(fn)并在该处理函数中维护this引用更合适。