如题,ajax中怎么判断div标签下是否存在img,
代码如下:要求若该div下不存在img则弹出“没有img”,否则不提示。我写了很久还是没有弄出来,肯定给出代码提示。 <div id="divtotal" name="divtotal"> <div id="showfile" name="divshowfile"> <img src="../../images/jpg.gif" alt="Sample" id="jpg.gif" />图片1 </div> </div> </tr><tr> <div id="divtotal" name="divtotal"> <div id="showfile" name="divshowfile"> </div> </div> </tr><tr> <div id="divtotal" name="divtotal"> <div id="showfile" name="divshowfile"> <img src="../../images/jpg.gif" alt="Sample" id="jpg.gif" />图片3 </div> </div> </tr><tr>
代码如下:要求若该div下不存在img则弹出“没有img”,否则不提示。我写了很久还是没有弄出来,肯定给出代码提示。 <div id="divtotal" name="divtotal"> <div id="showfile" name="divshowfile"> <img src="../../images/jpg.gif" alt="Sample" id="jpg.gif" />图片1 </div> </div> </tr><tr> <div id="divtotal" name="divtotal"> <div id="showfile" name="divshowfile"> </div> </div> </tr><tr> <div id="divtotal" name="divtotal"> <div id="showfile" name="divshowfile"> <img src="../../images/jpg.gif" alt="Sample" id="jpg.gif" />图片3 </div> </div> </tr><tr>
<div id= "divtotal " name= "divtotal ">
<div id= "showfile " name= "divshowfile ">
<img src= "../../images/jpg.gif " alt= "Sample " id= "jpg.gif " /> 图片1
</div>
</div>
</tr>
<tr>
<div id= "divtotal " name= "divtotal ">
<div id= "showfile " name= "divshowfile ">
</div>
</div>
</tr>
<tr>
<div id= "divtotal " name= "divtotal "> <div id= "showfile " name= "divshowfile ">
<img src= "../../images/jpg.gif " alt= "Sample " id= "jpg.gif " /> 图片3
</div>
</div>
</tr>
用js不是很容易判断吗?
var div_img = document.getElementById("id").getElementsByTagName("img");
之后再求div_img.length这个值,如果这个值不为0 ,则代表目标id的div框架下,有img的标签同时,id不能像你上面那样几个div写的一个id名称,id在每个文件中,是唯一的,否则容易出错的
<script type="text/javascript">
var divobj=document.geiElementById("divtotal");
var imgobjs=divobj.getElementsByTagName("img");
if(imgobjs.length<=0) alert("没有img");
</script>与ajax无关的问题,js判断实现
window.onload=function()
{
var divobj=document.getElementById("divtotal");
var imgobjs=divobj.getElementsByTagName("img");
if(imgobjs.length<=0) alert("没有img");
}
</script>
……这个是提示的是含有img的div数目;
我感觉用children判断也许能够实现,但是这个div是动态添加的,onload事件实在页面加载时判断的,这样,div明显实在onload事件之后,故你得方法没法判断。
能给一个好的解决办法么?谢谢