在我尝试将英文字母翻译成罗马数字之前,它(至少对我来说)很好。
我想翻译" 18"进入" XVII"和" 1212"进入" MCCXII"。还有,还有一种方法可以翻译" 1,212"进入" MCCXII"?
以下是所有代码:
<html>
<head>
<link rel="icon" href="http://claof.com/ClaofIcon.ico?v=2"></link/>
<title>English to Latin</title>
<body bgcolor="lightgray">
<center><input style="text-align:center;width:100%" placeholder="English text..." id="english"></input><br>
<a id="latin"></a></center>
</body>
<script>
xx=0
function replace_text() {
document.getElementById('english').value = document.getElementById('english').value.toUpperCase()
str = document.getElementById('english').value.toLowerCase()
if (document.getElementById('english').value.length) {
for (i=100000;i>0;i--) {
number = document.getElementById('english').value.match(/\d+/)[0]
v=i
v.toString()
if (number.match(v)) {
xx = toRoman(number.match(v))
}
}
}
str = str.split("").reverse().join("")
str = str.replace(/a/g, "");
str = str.replace(/b/g, "");
str = str.replace(/c/g, "");
str = str.replace(/d/g, "");
str = str.replace(/e/g, "");
str = str.replace(/f/g, "");
str = str.replace(/g/g, "G");
str = str.replace(/z/g, "");
str = str.replace(/h/g, "");
str = str.replace(/i/g, "");
str = str.replace(/j/g, "");
str = str.replace(/k/g, "");
str = str.replace(/l/g, "");
str = str.replace(/m/g, "");
str = str.replace(/n/g, "");
str = str.replace(/o/g, "");
str = str.replace(/p/g, "");
str = str.replace(/q/g, "");
str = str.replace(/r/g, "");
str = str.replace(/s/g, "");
str = str.replace(/t/g, "");
str = str.replace(/v/g, "");
str = str.replace(/w/g, "");
str = str.replace(/x/g, "");
str = str.replace(/y/g, "Y");
str = str.replace(/u/g, "");
document.getElementById('latin').innerHTML = str
}
setInterval("replace_text()",0)
function toRoman(num) {
var result = '';
var decimal = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
var roman = ["M", "CM","D","CD","C", "XC", "L", "XL", "X","IX","V","IV","I"];
for (var i = 0;i<=decimal.length;i++) {
// looping over every element of our arrays
while (num%decimal[i] < num) {
// keep trying the same number until it won't fit anymore
result += roman[i];
// add the matching roman number to our result string
num -= decimal[i];
// remove the decimal value of the roman number from our number
}
}
return result;
}
</script>
</head>
</html>
并且
答案 0 :(得分:0)
这是罗马数字转换器 Convert a number into a Roman Numeral in javaScript
删除“ setInterval(”replace_text()“,0)”而使用此“ document.getElementById('english')。addEventListener(”keyup“,replace_text); 强>“