Ich versuche, einen Code zu entwickeln, der in der Lage ist, die Schriftart eines Elements dynamisch zu ändern, indem eine Schriftartdatei (TTF) lädt. Der folgende Code funktioniert in Chrome, Opera und Firefox, funktioniert aber nicht in IE, Edge und Safari.Ändern Sie CSS font-face dynamisch mit JavaScript/JQuery
<html>
<head>
<style>
#my-font {
margin-top: 50px;
font-size: 20pt;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
$(function(){
$("#font").change(function(){
// post the font file to the php script that will move the file uploaded to the folder "fonts"
$.ajax({
url: 'movefontfile.php',
type: 'POST',
data: new FormData($("#send")[0]),
processData: false,
contentType: false,
}).done(function(name){
// set the font face
var f = new FontFace("myfont", "url(fonts/"+name+")", {});
f.load().then(function (loadedFace) {
document.fonts.add(loadedFace);
var fptags = document.getElementById('my-font');
fptags.style.fontFamily = "myfont, sans-serif";
});
});
})
});
</script>
</head>
<body>
<form id="send">
<input type="file" id="font" name="font">
</form>
<p id="my-font">
This is a font text
</p>
</body>
</html>
Dies ist der PHP-Code:
<?php
$name = $_FILES['font']['name'];
move_uploaded_file($_FILES['font']['tmp_name'], 'fonts/'.$name);
echo $name;
?>
mir jemand helfen könnte? Ich brauche einen Code, der in jedem gängigen Browser funktioniert. Vielen Dank.
Sie müssten wahrscheinlich die Seite neu laden in diesen Browsern. –