开发者

Loop through all elements with class name 'blah' in jquery and use the contents of the element in a function

This is what i'm trying to do in pseudo code

Find all <td> Elements with Class name 'OSGridRef'
For each <td> element pass the text of that <td> elemen开发者_如何学Ct into a function called ConvertToLatLong
Update an <td> element in the same  table row that has the class name 'LatLong' with the results of the function.

I haven't the foggiest how to do this in regular JavaScript or in jQuery.

Any Ideas?


This should do it. When you say 'content' I assume you mean all the text inside?

$(function() {
  $('tr:has(.OSGridRef)').each(function() {
    var content = $(this).find('.OSGridRef').text();
    var result = ConvertToLatLong($.trim(content));
    $(this).find('.LatLong').text(result);
  });
});


Hmm, this isn't going to be pretty considering that we're working with pseudocode only, but here goes:

$('.OSGridRef').each(function() {
    var $t = $(this);
    $t.closest('tr').find('.LatLong').text(
        ConvertToLatLong($t.text())
    );
});


Use each to do the iteration over the elements.

$(function() {
    $('.OSGridRef').each( function() {
        var content = $(this).text(), // or .html() if you need the HTML
            latlong = ConvertToLatLong(content),
            $latlongholder = $(this).closest('tr').find('.LatLong');
        $latlongholder.text(latlong);
    });
});


In jquery you can use selectors to find all elements needed

then you can use the each() function to be able to call your "ConverttoLatLong" function.

To update a element, you need to select it (with selector as seen before) then modify it value/text/etc.

The documentation on jQuery.com could help you a lot


$('.OSGridRef').each(function() {
  $('.LatLong').val(ConvertToLatLong($(this).val()));
})

this is my first guess, i don't know if they are inputs or something else, but if you could give more details (like html) i could edit this

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜