2014-10-02 58 views
1

我使用獲取元素的值一次,然後保持該值靜態

function expand(btn) { 
var box = btn.parentNode.parentNode, 
    ipsum = box.getElementsByTagName("p")[0], 
    textSize = window.getComputedStyle(ipsum, null).getPropertyValue('font-size'), 
    lineHeight = window.getComputedStyle(ipsum, null).getPropertyValue('line-height'), 
    boxWidth = window.getComputedStyle(box, null).getPropertyValue('width'), 
    initialHeight = window.getComputedStyle(box, null).getPropertyValue('height'), 
    numText = parseInt(textSize), 
    numWidth = parseInt(boxWidth), 
    numHeight= parseInt(initialHeight); 


if(box.style.height == "150px"){ 
    box.style.height = "40px"; 
    ipsum.style = "display:none"; 
} 
else{ 
    box.style.height = "150px"; 
    ipsum.style = "display:inline"; 
} 
console.log(lineHeight); 
} 

獲取元素的唯一問題是,元件高度頻繁變化的初始高度值,但所獲得的第一個值始終正確如何獲得初始值並保持靜態? 我如何只存儲變量中的值一次,我需要它在一個變量做計算,但隨着價值不斷變化我得到錯誤的數字輸出。

+2

好吧,除非你一直保存高度值到該變量中,你有你的初始值在那裏..也許我錯過了一些東西,你可以詳細說明問題(請在你的問題,請不要作爲評論) – myfunkyside 2014-10-02 03:05:52

回答

2

可重構功能將initialHeight存儲在一個 「私」 變量第一次它的運行:

var expand = (function() { 

    var initialHeight; 

    // Return a function that holds initialHeight in a closure 
    return function (btn) { 

     // Get box before setting/getting initialHeight 
     var box = btn.parentNode.parentNode; 

     // Set initialHeight only if undefined 
     initialHeight = initialHeight || window.getComputedStyle(box, null).getPropertyValue('height'); 

     // Do other variables 
     var ipsum = box.getElementsByTagName("p")[0], 
      textSize = window.getComputedStyle(ipsum, null).getPropertyValue('font-size'), 
      lineHeight = window.getComputedStyle(ipsum, null).getPropertyValue('line-height'), 
      boxWidth = window.getComputedStyle(box, null).getPropertyValue('width'), 
      numText = parseInt(textSize), 
      numWidth = parseInt(boxWidth), 
      numHeight= parseInt(initialHeight); 

     if(box.style.height == "150px"){ 
     box.style.height = "40px"; 
     ipsum.style.display = "none"; 

     } else { 
     box.style.height = "150px"; 

     // If ipsum is a P, probably better to use "" (empty string) here 
     // so it returns to its default or inherited value 
     // ipsum.style.display = "inline"; 
     ipsum.style.display = ""; 
     } 

     console.log(lineHeight); 
    } 
}()); 

以上是一個適當的重構,用以下標記進行測試:

<style type="text/css"> 
#box {border: 1px solid blue;} 
#notBox {border: 1px solid red;} 
#ipsum {border: 1px solid yellow;} 
</style> 
<div id="box">box 
    <div id="notBox">notBox 
    <input type="button" onclick="expand(this)" value="Expand&hellip;"> 
    <p id="ipsum">ipsum</p> 
    </div> 
</div> 
+0

此方法返回TypeError:Window.getComputedStyle的參數1不是一個對象。 – Plentybinary 2014-10-02 04:15:26

+0

@ Plentybinary-ok,我寫了一些HTML和CSS來測試它,只需要在設置* initialHeight *之前移動* box *。 – RobG 2014-10-02 06:06:47

+0

@RobG我有同樣的問題,看到你的答案後,我定義了我的變量像這樣:current_date = current_date ||這個。get_current_UTCDate();比它返回的'current_date'沒有定義。如果我正在刪除||條件,比它正常工作 – BomberMan 2014-11-02 17:04:08

0

每次執行功能expand時,都會爲initialHeight獲得一個新值。 所有你需要的是將它記錄在一個閉包中,如果你有超過1個btn來處理的話就用散列表示,如果你想爲每個btn記錄多個高度,就用數組賦值。就像這樣:

// predefine the function expand for furthre usage. 
var expand; 
(function() { 
    /* 
    having arrays as value, indexed like this: 
    { 
     <btn_1_id> : [<firstHeight>, <2nd Height>, ...], 
     <btn_2_id> : [], 
     ... 
    } 
    */ 
    // Let's assume every btn is having an id. You may think another way yourself it they don't. 
    var initialHeightForMultiBtns = {}; 
    expand = function(btn) { 
     // ...blablabla 
     initialHeightForMultiBtns[btn.id] = initialHeightForMultiBtns[btn.id] || []; 
     initialHeightForMultiBtns[btn.id].push(window.getComputedStyle(box, null).getPropertyValue('height')); 

     console.log(initialHeightForMultiBtns[btn.id][0]); // the real initialized height for the given btn. 
     // ...blablabla 
    } 
})() 

expand(btn_1); // let's expand btn_1 here. 

祝你好運。

+0

沒有多數民衆贊成在沒有它,價值仍然翻轉從40到150 – Plentybinary 2014-10-02 03:24:49

+0

對不起沒有看到您的更新。 我已經回答了一遍,希望這是你要找的。 – yarmyarch 2014-10-02 03:30:15

+0

@ yarmyarch- * initialHeight *不是全球性的,它被關閉並且是「私人的」。 – RobG 2014-10-02 03:51:28

1

你可以將它設置爲屬性嗎?喜歡的東西:

// set the value once some place 
box.setAttribute('data-init-height', window.getComputedStyle(…)…); 

// when setting the initial height, check for the attribute first 
initialHeight = box.getAttribute('data-init-height') || window.getComputedStyle(…)…; 

後續: see fiddle

+0

當使用此我得到TypeError:沒有足夠的參數引用框變量的Element.setAttribute。 – Plentybinary 2014-10-02 04:51:57

+0

@Plentybinary查看小提琴 – vol7ron 2014-10-03 04:53:45