问题
I am trying to create a button in javascript and assigning HTML entity code ⛨
to it. This is the HTML entity code of down arrow. Instead of showing the down arrow, the entire code is displayed in the button as is.
Below is how i am trying to achieve it
<!DOCTYPE html>
<html>
<head></head>
<body>
<script type="text/javascript">
var btn = document.createElement("input");
btn.setAttribute('type','button');
btn.setAttribute('value','▼');
document.body.appendChild(btn);
</script>
</body>
</html>
Below is the output of my code
I expect that the button should display down arrow but for some reason it is not showing.
回答1:
You should use innerHTML
instead of setAttribute:
var btn = document.createElement("input");
btn.setAttribute('type','button');
btn.innerHTML = '⛨';
document.body.appendChild(btn);
http://jsfiddle.net/dapx0nsy/
回答2:
Try replacing ⛨
with \u26E8
, try \u2193
for down arrow. JsFiddle, ref
<!DOCTYPE html>
<html>
<head></head>
<body>
<script type="text/javascript">
var btn = document.createElement("input");
btn.setAttribute('type','button');
btn.setAttribute('value','\u26E8');
document.body.appendChild(btn);
btn = document.createElement("input");
btn.setAttribute('type','button');
btn.setAttribute('value','\u2193');
document.body.appendChild(btn);
</script>
</body>
</html>
回答3:
Try like this,
<html>
<head></head>
<body>
<button>⛨</button>
<script type="text/javascript">
var btn = document.createElement("input");
btn.setAttribute('type','button');
btn.setAttribute('value','\u26E8');
document.body.appendChild(btn);
</script>
</body>
</html>
First button from DOM, Second one from Script.
来源:https://stackoverflow.com/questions/56526299/assigning-html-entity-code-in-javascript-button-not-working