<div>
<div id ="container">
<div>
<img src="some src>
</div>
<p>get this text</p>
<h>hello</h>
</div>
</div>
I want the total number of images inside a main div and want to extract html tags other than image tag.
OUTPUT:
No. of images =1
strings= <p>get this text</p><h>hello</h>
please give me a demo
尝试这个
$('#container').find('img').length
$("#container img").length
- For img
tag count
To extract strings for each,
$("#container").find("p, h").not("div").each(function() {
console.log($(this).get(0)); //Object
});
Note: You are using some weird element which is an invalid HTML tag, I think its <h>
<h1>
or some other header element.
DEMO: http://jsbin.com/nosin/5/edit
JS:
var a = $('#container').find('img').length;
console.log('No. of images =' + a);
var b = $('#container').find('h1, h2, h3, h4, h5, h6, p');
var c = '';
for(var i = 0; i < b.length; i++) {
c += b[i].outerHTML;
}
console.log('strings=' + c);
HTML:
<div>
<div id ="container">
<div>
<img src="some src"/>
</div>
<p>get this text</p>
<h1>hello</h1>
</div>
</div>
OUTPUT:
"No. of images =1"
"strings=<p>get this text</p><h1>hello</h1>"
Please check this one it will help you.
$(document).on("click", "#container", function(){
var img = $(this).find("img"), // select images inside container
len = img.length; // check if they exist
if( len > 0 ){
// images found, get id
var attrID = img.first().attr("src"); // get src attr of first image
} else {
// images not found
}
});
//For multiple images
var arr = []; // create array
if( len > 0 ){
// images found, get src
img.each(function(){ // run this for each image
arr.push( $(this).attr("src") ); // push each image's src to the array
});
} else {
// images not found
}
Try this :
<script type="text/javascript">
$(document).ready(function(){
var no_of_img = jQuery('#container img').length;
$("#container").find("*").each(function() {
var obj = $(this).get(0); //Object
if($(obj).find("img").length > 0 || $(obj).is("img")){
}else{
console.log($(obj).get(0));
}
});
});
</script>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.