2015-01-12 31 views
0

我正在嘗試並未能創建一個點擊功能來調整一些文本的大小。任何人都可以看到我做錯了什麼?我當然知道代碼是不正確的,因爲我在Javascript中是一個小菜鳥。 提前歡呼!試圖通過Javascript點擊調整文本大小,我做錯了什麼?

<div class="Appraisals" id="change"> 
    <div align="left"><span class="Block1">Random text that nobody cares about. 
Random text that nobody cares about.Random text that nobody cares about. </span></div> 
    <button type="button" 
onclick="document.getElementById('change').style.height "50px"; > 
Click Me!</button> 
</div> 
<div class="inspire"> <img src="inspire.jpg" alt="Crest" height="200"></div> 

回答

0

它應該是

document.getElementById('change').style.fontSize = '50px' 

代替:

document.getElementById('change').style.height "50px"; 

FIDDLE

+0

乾杯的答覆,我試過,並像以前一樣,沒有。 – user3071724

+0

你用正確的語法試過了嗎? – jmore009

+0

是的。即使使用你推薦的小提琴什麼也沒做,還有什麼提示? – user3071724

0

選項之一:

呼叫myFunction的的onclick並傳遞ID和字體大小,它們處理腳本文件中的操作。 (好)

HTML:

<div class="Appraisals" id="change"> 
     <div align="left"><span class="Block1">Random text that nobody cares about. 
     Random text that nobody cares about.Random text that nobody cares about.</span> 
     </div> 
     <button type="button" onclick="myFunction('change', '50px');" > Click Me!</button> 
     Click Me! 
     </button> 
    </div> 
    <div class="inspire"> <img src="inspire.jpg" alt="Crest" height="200"></div> 

JS:

<script> 
    function myFunction(element, clr) { 
    var changeId = document.getElementById(element); 
    changeId.style.fontSize=clr; 
    } 
</script> 

選項二:

手柄與HTML中的操作。 (不好)

<div class="Appraisals" id="change"> 
     <div align="left"><span class="Block1">Random text that nobody cares about. 
     Random text that nobody cares about.Random text that nobody cares about.</span> 
     </div> 
     <button type="button" onclick="document.getElementById("change").style.fontSize="50px"" > Click Me!</button> 
     Click Me! 
     </button> 
    </div> 
    <div class="inspire"> <img src="inspire.jpg" alt="Crest" height="200"></div> 

方案三:

使用jQuery。 (非常好)

首先調用jQuery的CDN:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> 

他們:

<script> 
    $(document).ready(function() { 
    $('button').click(function() { 
     $('#change').css('font-size', '50px'); 
    }); 
    }); 
</script> 
+0

我忘了關閉onclick,對不起.. –

+0

對不起,我可能聽起來像一個白癡在這裏,但是是「我的功能」,在那裏我把我的div id或...正如我所說,我是這個新的可憐! – user3071724

+0

不,它必須是JavaScript,否則我會看起來像一個噱頭作弊。我只是無法找到任何關於如何改變JavaScript中的文字大小。 – user3071724

0

你想改變文本的大小..不高?

document.getElementById('change').style = 'font-size:50px;' 
+0

是啊!就是這樣! – user3071724

相關問題