HTML 在 textarea 中不解释欧元字符的转义?

HTML escape for euro character not being interpreted in textarea?

它在文本区域中显示 € 而不是 €(货币符号)。有谁知道为什么会出错?

<?php
$currency = "&euro;"; //using php with other data from database

echo "<script>
      $('#share_button').click(function(e){
      // pass the currency to javascript and put in textarea shared with other on clicks
           $('#snsdescp').val(`".$currency."`);
      });</script>";
?>

 //shared textarea
<textarea class="form-control" name="message" id="snsdescp"></textarea> 

val() 方法没有 encoding/decoding,作为 hack 您可以使用 html() 函数进行编码,然后剥离文本:

$('#share_button').click(function(e){
    $('#snsdescp').val($("<div>").html("&euro;").text());
});

Here is a working jsFiddle 用于您的文本区域。

您可以通过将 &euro; 分配给 textarea 元素的 innerHTML 属性 来设置它:

document.querySelector('#snsdescp').innerHTML = '&euro;';

如果你想使用jQuery,你可以简单地调用它的.html()方法:

$('#snsdescp').html('&euro;');

好吧,我不明白为什么要存储在 php 变量中,但也许您可以存储在 javascript 变量中。它可以帮助你。

<?php
  $currency = "\u20ac"; //using php with other data from database
?>

 <textarea class="form-control" name="message" id="snsdescp"></textarea> 
  <br/><br/>
  <div id="share_button" >hello</div>//suppose your id is here

 <script>
    var value ="<?php echo $currency;?>"; //store php value in js variable
    $("#share_button").click(function(){

        $("#snsdescp").val(value);

     });
  </script>

您可以使用 Unicode 支付 € \u20AC

$('#snsdescp').val("\u20AC");