admin管理员组

文章数量:1333451

How to get number of HTML elements (HTML tags) in a string?

For example I have this string:

<div> some text
     <div>
           <label>some text</label>
     </div>
</div>

Now how would I find number of html tags in this string? The result in the case above will be 3.

I did tried this, thought it will work but it didn't:

$('*').length;

How to get number of HTML elements (HTML tags) in a string?

For example I have this string:

<div> some text
     <div>
           <label>some text</label>
     </div>
</div>

Now how would I find number of html tags in this string? The result in the case above will be 3.

I did tried this, thought it will work but it didn't:

$('*').length;
Share Improve this question asked Jan 23, 2013 at 4:58 atifatif 1,69313 gold badges38 silver badges70 bronze badges 3
  • Check below code is helpful or not.. – Sahal Commented Jan 23, 2013 at 5:06
  • I am confused. Why a string? Couldn't you just traverse the DOM with .each() and have an incrimentor output your total? – cliffbarnes Commented Jan 23, 2013 at 5:06
  • am giving a try to each of the answer @Sahal – atif Commented Jan 23, 2013 at 5:15
Add a ment  | 

6 Answers 6

Reset to default 4
var s = "<div> some text<div><label>some text</label></div></div>";   
var length = $(s).find('*').andSelf().length;

http://jsfiddle/jCW7Z/

If you're using jQuery (and it looks like you are), the easiest way would be to use code similar to the following:

var count = $( yourString ).find('*').andSelf().length

However, if you're using vanilla javascript, the implementation would look more like this:

var temp = document.createElement('div');
temp.innerHTML = yourString;

var count = temp.getElementsByTagName('*').length;
  1. Get all elements in the document (document.getElementsByTagName('*'))
  2. Do a regular expression match on the element's className attribute for each element

You can use jQuery: $("html *") which will return all elements between the html tags

  1. for names you must use $("html *").attr('name')
  2. for values $("html *").val() or $("html *").attr('value')

Possible way to do it: http://api.jquery./length/

check out Find number of element in string with jQuery

for example

$( '.myClass', myData ).length;

(although this might not show outermost tags)

You can do it like this,

Put the string in to some hidden div like

$('#myHiddenDiv').html(string);

then

You can gen number of children under it

This will tell you number of html elements under the body

var count = $("#myHiddenDiv> *").length;

or:

var count = $("#myHiddenDiv").children().length;

Your div will be like this

<div id="myHiddenDiv" style="display:none"></div>

A small, self documenting, example :)

<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>List Elements</title>
</head>
<body>
<div>some text
    <div>
        <label>some text</label>
    </div>
</div>
<script>
var x=document.getElementsByTagName('*');
console.log(x.length)
for (i=0;i<x.length;i++) {
    console.log(x[i]);
}
</script>
</body>

</html>

本文标签: javascriptHow to get number of html elements in a stringStack Overflow