2016-03-28 63 views
2

這是我現在擁有的,但按鈕仍然沒有調整文本大小。每個按鈕的值分別爲0.1和-0.1,字體大小目前設置爲1em。有任何想法嗎?使用javascript使圖像作爲字體大小調整按鈕

window.onload = startup; 

function startup() { 
    var fontButtons = document.getElementsByClassName("fontsizer"); 
    var i; 
    alert(fontButtons.length); 
    for (i = 0; i < fontButtons.length; i++) 
fontButtons[i].onclick = function(){resizeText(this)}; 
} 

function resizeText() { 
    var fontChange; 
    fontChange = parseFloat(objButton.value); 

    if (document.body.style.fontSize == "") { 
     document.body.style.fontSize = "1.0em"; 
    } 

    var currentFontSize; 
    alert("changed"); 
    currentFontSize = parseFloat(document.body.style.fontSize); 
    currentFontSize = currentFontSize+fontChange; 
    document.body.style.fontSize = "currentFontSize+em" 
} 

回答

0

要調用帶有參數的函數從onclick,把它放在一個匿名函數所以裏面:

fontButtons[i].onclick = resizeText(this);

變爲:

fontButtons[i].onclick = function(){resizeText(this)};

此外,請確保您以後運行startup().fontsizer元素已創建。例如window.onload = startup;或這樣的事情,你可以運行時:

document.onreadystatechange = function() { 
    if (document.readyState !== 'complete') 
     return; 

    startup(); 
} 

工作小提琴:https://jsfiddle.net/ckn9sLfv/

+0

我會放置在window.onload =函數(){啓動();}在整個的JavaScript頁面的頂部?對不起,我很新 – Jakeanake

+0

按鈕仍然無法正常工作,我的HTML有問題嗎? '

' – Jakeanake

+0

另外我把''作爲我的方法讓它首先在html中加載。 – Jakeanake

0
fontButtons[i].onclick = resizeText; // not (this); 

您需要分配功能,沒有返回值(未定義)。