Javascript 在div中选择图像
获取此文本 你好 我想要一个主div中的图像总数,并且想要提取除图像标记以外的html标记。Javascript 在div中选择图像,javascript,jquery,html,Javascript,Jquery,Html,获取此文本 你好 我想要一个主div中的图像总数,并且想要提取除图像标记以外的html标记。输出:图像数量=1字符串=获取此文本你好 请给我一个演示试试这个 <div> <div id ="container"> <div> <img src="some src> </div> <p>get this text</p> <
输出:
图像数量=1
字符串=
获取此文本你好
请给我一个演示试试这个
<div>
<div id ="container">
<div>
<img src="some src>
</div>
<p>get this text</p>
<h>hello</h>
</div>
</div>
$(“#集装箱img”)。长度
-用于img
标签计数
要提取每个字符串的字符串
$('#container').find('img').length
注意:您正在使用一些奇怪的
元素,它是一个无效的HTML标记,我认为它是
或其他一些标题元素。演示:
JS:
var a=$('#container').find('img').length;
console.log('图像数量='+a);
变量b=$(“#容器”)。查找('h1,h2,h3,h4,h5,h6,p');
var c=“”;
对于(变量i=0;i
HTML:
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);
获取此文本
你好
输出:
<div>
<div id ="container">
<div>
<img src="some src"/>
</div>
<p>get this text</p>
<h1>hello</h1>
</div>
</div>
“图像数量=1”
“字符串=获取此文本您好”
请检查这一项,它将帮助您
"No. of images =1"
"strings=<p>get this text</p><h1>hello</h1>"
试试这个:
$(文档).ready(函数(){
var no_of_img=jQuery('#container img')。长度;
$(“#容器”).find(“*”).each(function(){
var obj=$(this.get(0);//对象
if($(obj).find(“img”).length>0 | |$(obj).is(“img”)){
}否则{
log($(obj.get(0));
}
});
});
im我的html页面可能有div,但我只想要容器div中的图像。我不知道html页面中有哪个标记,所以我可以写这样的内容吗?查找(所有内容,但不是img)@SomPathak是的,所有内容,使用*
如果我们想在img标记出现时中断html字符串,您会看这个吗come@SomPathak这件事我们做完了吗?它解决了你的问题吗?是的,但我想要这样的东西。如果我们想在img标签出现时中断html字符串,你会看看这个吗?他想要的是文字字符串,而不是计数
"No. of images =1"
"strings=<p>get this text</p><h1>hello</h1>"
$(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>