jQuery,JavaScript检查某个元素在页面上是否存在

 参考链接:https://www.cnblogs.com/ww03/p/6004234.html

jQuery检查某个元素在页面上是否存在

// 应该根据获取元素的长度来判断,代码如下

if($("#tt").length > 0) {

    //元素存在时执行的代码
} 

JavaScript检查某个元素在页面上是否存在

//js判断元素是否存在

if(document.getElementById("tt")) {

    document.getElementById("tt").style.color = "red";
    document.getElementById("tt").stylebackgroundColor="green";
}

猜你喜欢

转载自blog.csdn.net/weixin_39907729/article/details/83339273