jquery如何判断某个元素是否存在(判断元素是否存在某个属性)

2025年02月27日 建站教程

jquery如何判断某个元素是否存在,判断元素中是否存在某个属性,下面web建站小编给大家详细介绍一下代码!

判断某个元素是否存在

//判断元素是否大于0
if($("li").length >0){
 console.log("元素存在");
}else{
 console.log("("元素不存在");
}

判断元素中是否存在某个属性

//根据typeof判断是否为undefined
if(typeof($(selector).attr(attrName))=="undefined"){
  console.log("属性不存在");
}else{
  console.log("属性存在");
}

本文链接:http://so.lmcjl.com/news/23949/

展开阅读全文