JavaScript会统计网页上的所有元素

时间:2015-08-18 13:40:45

标签: javascript

我希望能够计算包含某个字符串的网站上的所有内容,即使它没有显示在页面上。

我发现了这个:

window.occurrencesFunc()

我不确定如何使用它。我确实玩过它,但似乎没什么用。

这就是我的尝试:

function main() {
    function(r) {
        var amount = window.occurrencesFunc(r, "string", false);
        $('ul.reset').append('Count: ' + amount);
    }
}

main();

3 个答案:

答案 0 :(得分:3)

我将整个页面作为字符串:

var markup = document.documentElement.innerHTML;

然后,我会使用match方法匹配字符串中的字符串出现次数并计算它们:

//正则表达式中的g表示搜索单词(不是单词的一部分)

var resultArray= markup.match(/WORRDDD/g);

var count = resultArray.length

速记,将是

var count = (markup.match(/WORRDDD/g) || []).length;

答案 1 :(得分:0)

这样的功能怎么样?

function findOccurrences (string)
{
    var elements = document.getElementsByTagName ('*');
    var foundCount = 0;

    for (var i = 0, il = elements.length; i < il; i++) {
        if (!elements[i].textContent || !elements[i].textContent.match (string)) {
            continue;
        }

        foundCount++;
    }

    return 'Count: ' + foundCount;
}

像这样使用它,HTML ......

<div id="occurrenceCount"></div>

JavaScript ......

var countElement = document.getElementById ('occurrenceCount');
    countElement.textContent = findOccurrences ('your string');

这将使上面的<div>包含类似的内容:

Count: 35

答案 2 :(得分:0)

我可能会遍历DOM中存在的元素节点

          <!-- Modal HTML -->
         <div id="myModal" class="modal fade">
           <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Confirmation</h4>
            </div>
            <div class="modal-body">
                <p>Do you want to save changes you made to document before closing?</p>
                <p class="text-warning"><small>If you don't save, your changes will be lost.</small></p>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <a href="#myModal1" class="btn btn-primary" data-toggle="modal">Launch Demo Modal</a>
            </div>
        </div>
    </div>
</div>

       <!-- Modal HTML -->
        <div id="myModal1" class="modal fade">
            <div class="modal-dialog modal-lg">
                <div class="modal-content">
                    <div class="modal-header">
                  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Confirmation</h4>
                </div>
             <div class="modal-body">
                 <p>Do you want to save changes you made to document before closing?</p>
                  <p class="text-warning"><small>If you don't save, your changes will be lost.</small></p>
                <p>Do you want to save changes you made to document before closing?</p>
                <p class="text-warning"><small>If you don't save, your changes will be lost.</small></p>
            </div>
            <div class="modal-footer">
                <!--
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>-->
                <a href="#myModal1" class="btn btn-default" data-dismiss="modal">close</a>
                <button type="button" class="btn btn-primary">Save changes</button>
            </div>
        </div>
    </div>
      </div>
    </div>     
   </body>
  </html>      

用法:

function countElementsMatching(needle, elements) {

    return Array.prototype.slice.call(elements)
        .map(function (node) {
            return node.textContent;
        })
        .filter(function (str) {
            return str.indexOf(needle) !== -1;
        }).length;
}