315 vs 315.这些符号的html代码是什么?

时间:2017-06-28 12:06:29

标签: javascript character ascii special-characters symbols

我无法谷歌html代码的符号,如 315 看起来像数字 3 1 5。

有什么想法吗?谢谢。

1 个答案:

答案 0 :(得分:1)

在javascript中,您可以使用charCodeAt来获取角色的代码:



var char = prompt("Paste the character here: ");

var decimal = char.charCodeAt(0),
    hexa = decimal.toString(16);
    
console.log("Decimal code:", decimal);
console.log("Hexadecimal code:", hexa);




要将其作为文本添加到元素中,请使用:

element.innerHTML = "&#" + decimalCode + ";";



var p = document.querySelector("p"),
    char = "3";
    
p.innerHTML = "The char is: " + char + "<br>" + 
"The next char is: &#" + (char.charCodeAt(0) + 1) + ";";
&#13;
<p></p>
&#13;
&#13;
&#13;

不久前,我制作了一个代码,让我了解特定角色的周围角色:

&#13;
&#13;
var container = document.querySelector("#container"),
    inp = document.querySelector("#theChar"),
    before = document.querySelector("#before"),
    after = document.querySelector("#after");
    
document.querySelector("button").onclick = function() {
  container.innerHTML = "";
  var code = inp.value.charCodeAt(0),
      bef = +before.value,
      aft = +after.value;
  for(var i = code - bef; i <= code + aft; i++) {
    var div = document.createElement("div");
    div.innerHTML = "&#" + i + ";";
    container.appendChild(div);
  }
}
&#13;
#container div {
  display: inline-block;
  width: 50px;
  height: 50px;
  line-height: 50px;
  text-align: center;
  margin: 3px;
  border: 1px solid black;
}

label {
  display: block;
}
&#13;
<label>The character: <input id="theChar"></label>
<label>Characters before: <input type="number" id="before" value="10"/></label>
<label>Characters after: <input type="number" id="after" value="10"/></label>
<button>Show</button>
<div id="container"></div>
&#13;
&#13;
&#13;