按类选择多个元素的文本内容

时间:2014-03-15 11:19:42

标签: javascript jquery

我想做一个搜索演示。我的问题是我有一个父div,其中我有两个子div。两个子div都有相同的类(RLTRightDiv)。我想搜索所有内容。你能告诉我如何实现这一目标吗?它目前只搜索父div的第一个孩子。

示例“离开”出现在div。

http://jsfiddle.net/3MVNj/5/

var searchIndex = -1;
var searchTermOld = '';

$(document).ready(function () {
    /*
      Search Functionality method.
    */
    $('.searchbox').on('change', function () {
        if ($(this).val() === '') {
            var selector = "#fullContainer .RLTRightDiv";
            $(selector + ' span.match').each(function () {
                $(this).replaceWith($(this).html());
            });
        }
        searchIndex = -1;
        $('.searchNext').attr("disabled", "disabled");
        $('.searchPrev').attr("disabled", "disabled");
        searchTermOld = $(this).val();
    });
    $('.searchbox').on('keyup', function () {

        var selector = "#fullContainer .RLTRightDiv";
        if ($(this).val() === '') {
            $(selector + ' span.match').each(function () {
                $(this).replaceWith($(this).html());
            });
        }
        if ($(this).val() !== searchTermOld) {
            $(selector + ' span.match').each(function () {
                $(this).replaceWith($(this).html());
            });
            searchIndex = -1;
            $('.searchNext').attr("disabled", "disabled");
            $('.searchPrev').attr("disabled", "disabled");
        }
    });
    //Search Click method
    $('.search').on('click', function () {


        if (searchIndex == -1) {
            var searchTerm = $('.searchbox').val();
            if (searchTerm == '') {
                PG_alert("Please Insert Text.")
                return;
            }

            if (searchTerm == 'b'||searchTerm == 'br'||searchTerm == 'r') {
                return;
            }
            setTimeout(function(){
                searchAndHighlight(searchTerm);
            },300);
        } else
        {

            //naveen
            setTimeout(function(){
                searchNext();
            },300);
        }
        if ($('.match').length > 1) {
            $('.searchNext').removeAttr("disabled");
            $('.searchPrev').removeAttr("disabled");
        }
    });
    $('.searchNext').on('click', searchNext);

});

/*
  Seacrh and highlight text method.
*/
function searchAndHighlight(searchTerm) {
    if (searchTerm) {
        var searchTermRegEx, matches;
        var selector = "#fullContainer .RLTRightDiv";
        $(selector + ' span.match').each(function () {
            $(this).replaceWith($(this).html());
        });
        try {
            searchTermRegEx = new RegExp('(' + searchTerm + ')', "ig");
        } catch (e) {
            return false;
        }
        $('.highlighted').removeClass('highlighted');
        matches = $(selector).text().match(searchTermRegEx);
        if (matches !== null && matches.length > 0) {
            var txt = $(selector).html().replace(searchTermRegEx, '<span class="match">$1</span>');
            $(selector).html(txt);
            searchIndex++;
            $('.match:first').addClass('highlighted');
            $('#realTimeContents').animate({
                scrollTop: $('.match').eq(searchIndex).get(0).offsetTop
            });

            return true;
        } else {
            console.log("yes===========")
            searchIndex = -1;
        }
        return false;
    }
    return false;
}

function searchNext() {
    var searchTerm = $('.searchbox').val();
    if (searchTerm == '') {
        PG_alert("Please Insert Text.")
        return;
    }
    if (searchTerm == 'b'||searchTerm == 'r'||searchTerm == 'br') {
        // PG_alert("Please .")
        return;
    }

    if(searchIndex!=-1){
        searchIndex++;
        if (searchIndex >= $('.match').length) {
            //naveen end
            searchIndex = -1;
        }
        $('.highlighted').removeClass('highlighted');
        $('.match').eq(searchIndex).addClass('highlighted');
        $('#realTimeContents').animate({
            scrollTop: $('.match').eq(searchIndex).get(0).offsetTop
        });
    }
}

0 个答案:

没有答案