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

javascript - How to loop through all elements in a div and find tags that contain text - Stack Overflow

programmeradmin3浏览0评论

I tried to loop through a div that contains all kind of elements. The problem is, that I get the same results showing up in my example.

Here is the fiddle with the example code: /

So the html structure of my example looks like:

<div class="somediv">
    <span>hello</span>
    <img src="someimg.jpg" width="20" height="20" />
    <p></p>
    <div><p>some text in a paragraph inside a div</p></div>
    <h2>Some h2 tekst</h2>
    <table>
        <tbody>
            <tr>
                <td>some text in a table cell</td>
            </tr>
        </tbody>
    </table>
</div>

My goal was to get all the content inside the html tags. like so,

tagtype: <span>, text: 'hello',
tagtype: <p>, text: 'some text in a paragraph inside a div'
etc.

Does anyone know how to do this correctly?

I tried to loop through a div that contains all kind of elements. The problem is, that I get the same results showing up in my example.

Here is the fiddle with the example code: http://jsfiddle/JLtCN/

So the html structure of my example looks like:

<div class="somediv">
    <span>hello</span>
    <img src="someimg.jpg" width="20" height="20" />
    <p></p>
    <div><p>some text in a paragraph inside a div</p></div>
    <h2>Some h2 tekst</h2>
    <table>
        <tbody>
            <tr>
                <td>some text in a table cell</td>
            </tr>
        </tbody>
    </table>
</div>

My goal was to get all the content inside the html tags. like so,

tagtype: <span>, text: 'hello',
tagtype: <p>, text: 'some text in a paragraph inside a div'
etc.

Does anyone know how to do this correctly?

Share Improve this question edited Mar 28, 2013 at 17:44 Elias Zamaria 101k34 gold badges121 silver badges152 bronze badges asked Mar 27, 2013 at 21:34 chriscrosswebchriscrossweb 3481 gold badge5 silver badges23 bronze badges 1
  • It's not clear to me what it is you want to achieve. Have you tried just calling $('.somediv').text()? – Blazemonger Commented Mar 27, 2013 at 21:35
Add a ment  | 

3 Answers 3

Reset to default 3

Try this:

$('.somediv *').each(function() {
    var hastext = $(this).text().length != 0;
    if(hastext) {
        console.log("type: " + $(this)[0].tagName + " text: " + $(this).text());
    }
});

DEMO: http://jsfiddle/JLtCN/1/

If you want to get rid of the dupes, simply store the logged items in an array and then check if it's not in the array before logging, something like:

Also, it now does them in reverse order so you get the lowest child instances only:

var addedItems = new Array();

$.each($('.somediv *').toArray().reverse(), function() {
    var hastext = $(this).text().length != 0;
    if(hastext && addedItems.indexOf($.trim($(this).text())) == -1) {
        addedItems.push($.trim($(this).text()));
        console.log("type: " + $(this)[0].tagName + " text: " + $(this).text());
    }
});

DEMO: http://jsfiddle/JLtCN/3/

Something like this:

var seenStrings = [],
    content = $.map(
        $(".somediv *"),
        function(element) {
            var text = $(element).text();
            if ($.inArray(text, seenStrings) !== -1) {
              return {
                  tagname: "<" + element.nodeName.toLowerCase() + ">",
                  text: $(element).text()
              };
            } else {
              return null;
            }
        }
    );
var walk_the_DOM = function walk(node, func) {
    func(node);
    node = node.firstChild;
    while (node) {
        walk(node, func);
        node = node.nextSibling;
    }
};

walk_the_DOM(document.body, function(node) {
    if(node.nodeType == 1){
        document.write(node.nodeName + ": " );
    }        
    if(node.nodeType == 3){
        document.write(node.nodeValue +" <br/>");
    }
});
发布评论

评论列表(0)

  1. 暂无评论