【问题标题】:Changing attribute value in its every appearance in jQuery,在 jQuery 中每次出现时更改属性值,
【发布时间】:2016-04-27 22:31:25
【问题描述】:

我尝试将每个<a> 元素的属性“数据发布”的值更改为由更改日期格式的 romString 函数更改的值。我做错了什么?

<script>


      $(function(){       

        var id='139540263@N06';
        var tag = document.getElementById('html').getAttribute("data-tag");


        // Flickr Photostream feed link.
        $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?id=" + id + "&tags="+ tag + "&lang=en-us&format=json&jsoncallback=?", 

            function(data){$.each(data.items, 

                function(i,item){


                    // Number of images to show.            
                    if(i < 4){

                    var newTaken=new Date(item.date_taken.replace("T"," ").replace("Z","").replace("-08:00","")).getTime();
                    alert(newTaken);
                    // Create images and append to div id flickr and wrap link around the image.
                    $("<img/>").attr("src", item.media.m.replace('_z', '_c')).appendTo("[data-tag=html]").wrap("<a href='" + item.media.m.replace('_z', '_c') + "' name='"+ item.link + "' title='" +  item.title + "'data-taken='" + newTaken + "'data-published='" + item.published.replace("T"," ").replace("Z","") + "'></a>");



                    }

                }); 

            }); 

        });



    </script> 

现在它会提醒 newTaken 是 NaN。你知道为什么吗?

【问题讨论】:

  • 尝试删除 'value' value=romString('value'); 周围的引号
  • 还是不行。 ://
  • 可以在 Question 中包含 html$(this).attr('data-published') 字符串示例?另外,尝试将.getTime() 移动到new Date() 的右括号之后;而不是链接到.match() 返回的最后一个值
  • 你这是什么意思?
  • 见下面的帖子,比较 a 元素 html at stacksnipetts 和 a element html at stackoverflow.com/questions/34927819/… 之间的差异

标签: jquery validation date attributes


【解决方案1】:

html 出现格式错误

&lt;a data-published="2016-01-20 11:15:49" data-taken="2010-01-20 03:15:49" title="3" name="flickr.com/photos/139540263@N06/24130487059/"; href="farm2.staticflickr.com/1665/…; &lt;img src="farm2.staticflickr.com/1665/…; &lt;/a&gt;

不应在属性值分配结束时包含;;在属性值处缺少关闭 "


.getTime() 移至new Date() 关闭后;而不是parts的匹配结果的最后索引

function romString(dateAsString){
    var parts = dateAsString.match(/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/);
    return new Date(parts[1],
                parts[2] - 1,
                parts[3],
                parts[4],
                parts[5],
                parts[6]).getTime();
}
$('.gallery a').each(function() {
    var value = $(this).attr('data-published');
    var new_value=romString(value);
    $(this).attr('data-published', new_value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<div class="gallery">
<a data-published="2016-01-20 11:15:49" data-taken="2010-01-20 03:15:49" title="3" name="flickr.com/photos/139540263@N06/24130487059/" href="farm2.staticflickr.com/1665/…"> <img src="farm2.staticflickr.com/1665/…" /> </a>
  </div>

function romString(dateAsString){
    var parts = dateAsString.match(/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/);
    return new Date(parts[1],
                parts[2] - 1,
                parts[3],
                parts[4],
                parts[5],
                parts[6]).getTime();
}
$('.gallery a').each(function() {
    var value = $(this).attr('data-published');
    var new_value=romString(value);
    $(this).attr('data-published', new_value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<div class="gallery">
<a data-published="2016-01-20 11:15:49" data-taken="2010-01-20 03:15:49" title="3" name="flickr.com/photos/139540263@N06/24130487059/" href="farm2.staticflickr.com/1665/…"> <img src="farm2.staticflickr.com/1665/…" /> </a>
  </div>

【讨论】:

  • 我没有;我不知道他们是怎么到那里的。
  • @DominikGłowicki 在 stacksn-ps 尝试过 js 吗?
【解决方案2】:

为什么不这样做

$('.gallery>a').each(function() {
  var dt = $(this).data('published');
  $(this).data('published',new Date(dt).getTime());
});

假设格式为

$(function() {
  $(".dd").each(function() {
    var dt = $(this).data("published");
    $(this).data("published",new Date(dt).getTime());
    $(this).text($(this).data("published"));
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="dd" data-published="2015-01-21 16:43:00"></div>
<div class="dd" data-published="2016-01-21 16:43:00"></div>

【讨论】:

  • data-published="2015-01-21 16:43:00" 的格式是这样的。但它仍然不起作用......真令人沮丧......
  • 也许你需要在连接 item.published 的地方做日期工作
  • item.published.replace("T"," ").replace("Z","") 更改为 new Date(item.published.replace("T"," ").replace("Z","")).getDate()
  • 请做一个console.log(item.published.replace("T"," ").replace("Z","")) 告诉我结果
  • 2016-01-20 11:15:49 - 控制台结果。看起来是对的,但是当我给它 Date()...getDate() 时,它表明 data-published=NaN。
猜你喜欢
  • 2017-12-06
  • 1970-01-01
  • 2011-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多