this question has answer here:
my app.js written in es6 standard , question > how can use code this?:
$('img').each(() => { var title = $(this).attr('src'); titles.push(title); });
i observed node compile code to:
'use strict'; $('img').each(function () { var title = $(undefined).attr('src'); titles.push(title); });
compile 'this' 'undefined', how can fix that? thx in advance ;)
another solution use element passed callback, defined in .each documentation - function ( integer index, element element ):
var titles = []; $('img').each((index, element) => { var title = $(element).attr('src'); titles.push(title); }); document.getelementbyid('demo').innerhtml = json.stringify(titles, undefined, 4);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <img src="http://38.media.tumblr.com/avatar_502c67b98956_64.png"> <img src="https://s-media-cache-ak0.pinimg.com/favicons/62c7e8f531e824f6f8da34453cdd698a8856f3aea36118408c3e5c09.png?9e884f0299eee37aedab60fe1ed363b5"> <pre id="demo"></pre>
Comments
Post a Comment