2016-06-09 61 views
1

我需要用複選框來切換輸入標籤的字體,我嘗試過使用javascript和@ font-face。如何更改輸入的字體?

HTML:

<form action="#" method="post"> 

<input id="comment-input" type="text" class="form-control input-sm" placeholder="Press enter to post comment">     

<span class="input-group-addon sinhala"> 
Sinhala 
<input type="checkbox" aria-label="..." value="si" id="sinhala-toggle-checkbox"> 
</span> 


<button type="button" class="btn btn-sm" data-toggle="tooltip" title="" data-original-title="Mark as read">Post</button> 

</form> 

<script> 
$("#sinhala-toggle-checkbox").click(function(){ 
$("#comment-input").toggleClass("sinhala"); 
}); 
</script> 

CSS:

@font-face {font-family: sinhala; 
    src: url("malithi_web.ttf") format("truetype"); 
} 

.sinhala[type='text']{ 
    font-family: "Malithi web" !important; 
    font-size:16px; 
    font-style:normal; 
} 
+0

只有字體大小的變化,而不是字體 – mzmarkib

+0

順便說一句我試圖從服務器它自己加載字體(自定義) – mzmarkib

+0

你認爲我的瀏覽器/電腦有問題?我甚至嘗試禁用直接寫在鉻上。 – mzmarkib

回答

0

您可以在不同類的新字體。然後切換這個類。

.newFontClass{ 
     font-family: "Some-New-Font" !important; 
    } 

    $("#sinhala-toggle-checkbox").click(function(){  
     if($(this).is(':checked')) { 
      $("#comment-input").addClass("newFontClass"); 
     }else{ 
     $("#comment-input").removeClass("newFontClass"); 
     } 
    }); 
+0

試過了,bt仍然沒有改變字體。 也許是因爲字體是unicode? – mzmarkib

+0

thanx傢伙,嘗試了一個不是unicode的字體,並且與所有的建議一起工作。 – mzmarkib

0

請檢查您在代碼中添加正確的jQuery CDN: -

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

如果CDN是正確的,那麼只需更換我發佈你的腳本: -

SCRIPT:

$(document).ready(function(){ 
    $("#sinhala-toggle-checkbox").click(function(){ 
    $("#comment-input").toggleClass("sinhala"); 
    }); 
}); 

剩餘的整個代碼是正確的。

+0

如果您無法編輯代碼,那麼您如何更正代碼。我發佈的代碼是問題的解決方案。 –

+0

我的意思是說代碼很好,你只需要添加我在代碼中發佈的腳本。 –

+0

試過了,bt仍然沒有改變字體。也許是因爲字體是unicode? – mzmarkib