return FALSE; $r = well_tag_thread__update(array('id' => $id), $update); return $r; } function well_tag_thread_find($tagid, $page, $pagesize) { $arr = well_tag_thread__find(array('tagid' => $tagid), array('id' => -1), $page, $pagesize); return $arr; } function well_tag_thread_find_by_tid($tid, $page, $pagesize) { $arr = well_tag_thread__find(array('tid' => $tid), array(), $page, $pagesize); return $arr; } ?>jquery - Javascript get class text inside element - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Javascript get class text inside element - Stack Overflow

programmeradmin1浏览0评论

I have a bunch of span4 class elements in my html. they look something like this:

    <div class="span4">
        <div class="widget">
            <div class="header">blablabla</div>
        </div>
    </div>

I want to sort the span4 by that text iside header class.

I do this to sort them

$(".span4").sort(sortAlpha)

but how do I select the text inside the header class?

I'm doing this but I guess there is a better way

    function sortAlphaAsc(a,b){  
        var nomeA = $(a.childNodes[1].childNodes[1]).text();
        var nomeB = $(b.childNodes[1].childNodes[1]).text();
        return a.innerHTML.toLowerCase() > b.innerHTML.toLowerCase() ? 1 : -1;  
    };

there must be a better way than

$(a.childNodes[1].childNodes[1]).text()

I have a bunch of span4 class elements in my html. they look something like this:

    <div class="span4">
        <div class="widget">
            <div class="header">blablabla</div>
        </div>
    </div>

I want to sort the span4 by that text iside header class.

I do this to sort them

$(".span4").sort(sortAlpha)

but how do I select the text inside the header class?

I'm doing this but I guess there is a better way

    function sortAlphaAsc(a,b){  
        var nomeA = $(a.childNodes[1].childNodes[1]).text();
        var nomeB = $(b.childNodes[1].childNodes[1]).text();
        return a.innerHTML.toLowerCase() > b.innerHTML.toLowerCase() ? 1 : -1;  
    };

there must be a better way than

$(a.childNodes[1].childNodes[1]).text()
Share Improve this question asked Dec 14, 2012 at 16:21 andrepcgandrepcg 1,3317 gold badges26 silver badges45 bronze badges 3
  • 1 $('.span4').find('.header').text()? – bozdoz Commented Dec 14, 2012 at 16:26
  • 2 How about a.find('.header').text()? – Heretic Monkey Commented Dec 14, 2012 at 16:26
  • You don't use that variables in your return statement, simply remove them :). – Ram Commented Dec 14, 2012 at 16:26
Add a ment  | 

4 Answers 4

Reset to default 3
var elems = $(".span4");

elems.sort(function(a, b) {
    return $(a).find('.header').text().toUpperCase().localeCompare(
         $(b).find('.header').text().toUpperCase()
    );
});

$(".span4").parent().html(elems);​

FIDDLE

Try this:

function sortAlphaAsc(a,b){  
    var nomeA = $(a).find('div.header').text();
    var nomeB = $(b).find('div.header').text();
    return nomeA.toLowerCase() > nomeB.toLowerCase();  
};

You could detach the spans, sort and append them. That will be very fast too as changing elements in memory and only updating the DOM once in the end is very efficient.

var $spans = $(".span4").detach();

var sortedSpans = $spans.sort(function(spanA, spanB) {
    var spanTextA = $("div.header", spanA).text();
    var spanTextB = $("div.header", spanB).text();
    
    return spanTextA > spanTextB;
});

$("body").append(sortedSpans);

Obviously instead of body you append it back to it's actual container element.

Or if the spans are in a mon container store the parent in cache var $parent = $spans.parent() and in the end simply do $parent.html(sortedSpans).

I don't know your whole mark-up but that should get you started.

DEMO - Detach spans, sort them and append again

Do you mean something like this:

$('.span4').find('.header').text();

This will return the text inside the header div.

发布评论

评论列表(0)

  1. 暂无评论