最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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> element 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 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> element 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?

Share Improve this question edited Sep 28, 2011 at 13:15 MrBliz asked Sep 28, 2011 at 13:02 MrBlizMrBliz 5,92816 gold badges60 silver badges84 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 3

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. 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)

  1. 暂无评论