[英]Increase and decrease font size not working with all paragraphs javascript
**I have many separated paragraphs in one HTML page all of them have the same id **id=text** when I use the onmousemove event only the first paragraph can increase and decrease the font size not all of them although they have the same id .**
這是 javascript 代碼:
function incfont(){
var t= document.getElementById('fontsize').value;
var x= document.getElementById('text');
x.style.fontSize = t+"px";
**這里是具有相同 id 的段落示例 **
<p id="test">paragraph 1 </p>
<p id="test">paragraph 2 </p>
在 Html
<p class="test">paragraph 1 </p>
<p class="test">paragraph 2 </p>
在腳本中
function incfont(){
var t= document.getElementById('fontsize').value;
var x= document.getElementsByClassName("test");
for(var i=0;i<x.length;i++){
x[i].style.fontSize = t+"px";
}
}
在 Html
//Instead of Id use class in HTML
<p class="test">paragraph 1 </p>
<p class="test">paragraph 2 </p>
在腳本中
function incfont(){
var t= document.getElementById('fontsize').value;
//Here you need to change getElemetById to getElementsByClassName because getElementById() only gets a single element
var x= document.getElementsByClassName("test");
x.style.fontSize = t+"px";
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.