JavaScript如何判断节点是否存在


本文摘自PHP中文网,作者醉折花枝作酒筹,侵删。

方法:1、js方法,“if(document.getElementById('元素对象'))”;2、jquery方法,“if($('元素对象').length>0)”和“if($('元素对象')[0])”。

本教程操作环境:windows7系统、javascript1.8.5版、Dell G3电脑。

前两天工作时遇到一问题,就是模块A显示时,B是一种样式,模块A删除,B是另一种样式。记录下判断节点存在的方法。

先写下html

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

<!doctype html>

<html>

    <head>

        <meta http-equiv="Content-Type" content="text/html charset=utf-8" />

        <meta http-equiv="X-UA-Compatible" content="IE=edge chrome=1" />

        <meta name="keyword" content="随机加判断存在" />

        <meta name="description" content="" />

        <title>判断节点存在</title>

        <style type="text/css">

            *{margin: 0;padding: 0;}

            #box1{width: 100px;height: 100px;background: #c66;margin: 20px auto;text-align: center;color: #fff;line-height: 100px;}

            .box2{width: 200px;height: 200px;background: #c60;margin: 0 auto;text-align: center;color: #fff;line-height: 200px;}

            .box22{width: 400px;height: 400px;line-height: 400px;}

        </style>

    </head>

    <body>

 

        <div class="box2">模块二</div>

        <div id="box1">模块一</div>

        </body>

    </html>

判断id为box1的p是否存在的方法

js方法

if(document.getElementById('box1'))

jquery方法

1.if($('#box1').length>0)

2.if($('#box1')[0])

阅读剩余部分

相关阅读 >>

快速入门createjs实例教程

javascript中dom的含义是什么

h5里js和servlet实现文件上传的实现步骤

exports和module.expors之间有什么区别及联系?

pop()方法怎么用

math是javascript内置函数吗

javascript web workers的构建块及5个使用场景

javascript和css的区别是什么

javascript中添加事件到脚本被覆盖

理解js原型链,继承和promis

更多相关阅读请进入《javascript》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...