2012-05-30 63 views
1

我對JavaScript很新,並且一直都停留在這一天。我有一個腳本,根據屏幕res resize元素來調整它,除了我需要「如果元素不存在於文檔上,然後將一個addittional值」242px「添加到contentContainer.css方法」這是我迄今爲止這樣這eleemnt佔用asideContent元素時,其頁面上沒有的空間...javascript檢查元素以應用值的文檔

  <script> 

      // DOM Container Resize Handler 

       var extra = ($("body.asideContent").length == 0) ? 242 : 0; 

       var adjustStyle = function() { 
       var winWidth = $(window).width(), 
        width = parseInt(winWidth), 
        container = $('body .fixed'); 
       contentContainer = $('body .content'); 
        if (width >= 1454) { 
         container.css('width','1454px'); 
         contentContainer.css('width',extra + 1210 + "px"); 
        } else if ((width < 1454) & (width >= 1212)) { 
         container.css('width','1212px'); 
         contentContainer.css('width',extra + 968 + "px"); 
        } else { 
         container.css('width','970px'); 
         contentContainer.css('width',extra + 726 + "px"); 
        } 
       }; 
       $(function() { 
        var filterWrap = $("#filterWrap"), 
         offset = filterWrap.offset(), 
         topPadding = 15; 

        $(window).scroll(function() { 
         if ($(window).scrollTop() > offset.top) { 
          filterWrap.stop().animate({ 
           marginTop: $(window).scrollTop() - offset.top + topPadding 
          }); 
         } else { 
          filterWrap.stop().animate({ 
           marginTop: 0 
          }); 
         }; 
        }); 

        // DOM Container Resize Handler - Initialization  
        $(window).resize(function() { 
         adjustStyle(); 
        }); 
        adjustStyle(); 
       }); 
      </script> 

其應用額外的所有的時間,加上我只想將此時(「body.asideContent」)是沒有在文檔中

測試HTML

  <html> 
      <head> 
      </head> 
      <body> 
       <div class="fixed"> 
        <div id="container"> 
         <div class="asideContent">&nbsp</div> 
         <div class="content">&nbsp</div> 
        </div> 
       </div> 
      </body> 
      </html> 

只想提前說出感謝您的任何幫助,您可以給我......它一直在瘋狂地把我整天瘋狂......

回答

1

body.asideContent意味着帶有一類asideContent的身體。嘗試

$("body .asideContent") 

(又名,閥體和.asideContent之間的空間)

+0

它仍然增加242時.asideContent在頁面:( – user1349760