2017-04-26 94 views
2

首先我爲我的英語道歉。如果編輯們糾正我的錯誤,我很高興。如何更改div中的所有字體大小?

在這裏我的div和內容。

<div data-x="182" data-y="201" class="resize-drag" contenteditable="true" id="text-giris2" style="width: 115px; height: 153px; transform: translate(182px, 201px); position: absolute; text-align: left; overflow: hidden; border: 1px solid black;"> 
    <u style="font-size: 15px;">asd</u> 
    as a<b>sd</b> 
    <span>a<span style="color: red; font-size: 18px;">s a</span>sdas a 
    <span><span style="font-size: 18px;">sdasd 
    <span style="font-size: 26px;">addddddd</span></span></span></span> 
    </div> 

我想用唯一名稱「text-input2」動態更改div元素中所有字體大小的大小。但是,它會發生什麼不明確。如上所述,像<span>,<u>,<b>這樣的標籤可以是類似的。我想採取所有我想要做的字體大小的2倍。舉個例子,我想有<span style = "font-size: 18px>之後的<span style =" font-size: 36px>同樣,<u style = "font-size: 15px;">之後<u style = "font-size: 30px;">這樣的事情可以用CSS來完成嗎?如果這是不可能的,我會嘗試使用Javascript,但我不知道從哪裏開始。我在等你的幫助。謝謝。

+0

是JQuery的允許嗎? –

+0

ye當然。 @TobyMellor –

+0

你只需要這個CSS或者可以有更多的可能性? –

回答

2

我在下面提供的代碼將遍歷所有包含在div中的元素,其編號爲​​。在每次迭代中,它將採用當前字體大小並加倍。

$('#text-giris2').children().css('font-size', function(i, current){ 
    return parseFloat(current) * 2; 
}); 

一個例子

$('#container').children().css('font-size', function(i, current){ 
 
    console.log('New font size for ' + $(this).text() + ' is ' + parseFloat(current) * 2); 
 
    return parseFloat(current) * 2; 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="container"> 
 
    <span style="font-size: 5px;">Hello</span> 
 
    <p style="font-size: 7px;">World</p> 
 
    <span>Foobar</span> 
 
</div>

+1

Ty sir你救了我的一天:) –

+1

你可以重寫(爲簡潔)爲:'$('#container')。children()。css('font-size' ,function(i,current){return parseFloat(current)* 2;});'因爲jQuery的setter方法迭代集合中的每個元素,所以不需要使用'each()',並且'this'在匿名函數指向當前DOM節點,而「當前」變量指的是要設置或更新的屬性的當前值(在操作之前)。 –

+0

@DavidThomas像魅力一樣工作!感謝您的建議,我不知道這一點 –