Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/447.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在div中选择图像_Javascript_Jquery_Html - Fatal编程技术网

Javascript 在div中选择图像

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> <

获取此文本

你好 我想要一个主div中的图像总数,并且想要提取除图像标记以外的html标记。
输出:
图像数量=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;i0 | |$(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>