码迷,mamicode.com
首页 > Web开发 > 详细

js,jquery判断某一节点是否存在

时间:2014-11-21 18:07:16      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   ar   color   sp   java   

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

先写下html

 1 <!doctype html>
 2 <html>
 3     <head>
 4         <meta http-equiv="Content-Type" content="text/html charset=utf-8" />
 5         <meta http-equiv="X-UA-Compatible" content="IE=edge chrome=1" />
 6         <meta name="keyword" content="随机加判断存在" />
 7         <meta name="description" content="" />
 8         <title>判断节点存在</title>
 9         <style type="text/css">
10             *{margin: 0;padding: 0;}
11             #box1{width: 100px;height: 100px;background: #c66;margin: 20px auto;text-align: center;color: #fff;line-height: 100px;}
12             .box2{width: 200px;height: 200px;background: #c60;margin: 0 auto;text-align: center;color: #fff;line-height: 200px;}
13             .box22{width: 400px;height: 400px;line-height: 400px;}
14         </style>
15     </head>
16     <body>
17         
18         <div class="box2">模块二</div>
19         <div id="box1">模块一</div>
20         </body>
21     </html>

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

js方法

if(document.getElementById(‘box1‘))

jquery方法

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

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

 

放到代码里

 1 <script type="text/javascript">
 2             var number = (1+Math.random()*(8-1)).toFixed(0);
 3             var oBox2=document.getElementsByTagName(‘div‘)[0];
 4             var oBox1=document.getElementById(‘box1‘);
 5             if(number<3){
 6                 document.body.removeChild(oBox1);
 7             }
 8             if(document.getElementById(‘box1‘)){
 9                 oBox2.className=oBox2.className+‘ box22‘;
10                 console.log(111);
11             }
12             else{
13                 oBox2.className=‘box2‘;
14             }
15         </script>

jquery方法

 1 <script src="jquery-1.8.3.min.js"></script>
 2         <script type="text/javascript">
 3             var number = (1+Math.random()*(8-1)).toFixed(0);
 4             if(number>3){
 5             }
 6             else{
 7                 $(‘#box1‘).remove();
 8             }
 9             if($(‘#box1‘).length>0){//判断
10                 $(‘.box2‘).addClass(‘box22‘);
11             }
12             else{
13                 $(‘.box2‘).removeClass(‘box22‘);
14             }
15         </script>
<script src="jquery-1.8.3.min.js"></script>
        <script type="text/javascript">
            var number = (1+Math.random()*(8-1)).toFixed(0);
            if(number>3){
            }
            else{
                $(‘#box1‘).remove();
            }
            if($(‘#box1‘)[0]){//判断
                $(‘.box2‘).addClass(‘box22‘);
            }
            else{
                $(‘.box2‘).removeClass(‘box22‘);
            }
        </script>

每天进步一点点,努力超越昨天的自己。

 

js,jquery判断某一节点是否存在

标签:des   style   blog   http   io   ar   color   sp   java   

原文地址:http://www.cnblogs.com/MissBean/p/4113046.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!