Javascript 文本区域中未解释欧元字符的HTML转义?
它显示的是Javascript 文本区域中未解释欧元字符的HTML转义?,javascript,jquery,euro,Javascript,Jquery,Euro,它显示的是&欧元而不是文本区域中的€(货币符号)。有人知道为什么会出问题吗 <?php $currency = "€"; //using php with other data from database echo "<script> $('#share_button').click(function(e){ // pass the currency to javascript and put in textarea shared w
&欧元代码>而不是文本区域中的€(货币符号)。有人知道为什么会出问题吗
<?php
$currency = "€"; //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()
不进行编码/解码,作为黑客,您可以使用html()
函数进行编码,然后去除文本:
$('#share_button').click(function(e){
$('#snsdescp').val($("<div>").html("€").text());
});
$(“#共享按钮”)。单击(功能(e){
$('#snsdescp').val($(“”).html(“&euro;”).text());
});
用于您的文本区域。您可以通过分配&欧元来设置它
到textarea
元素的innerHTML
属性:
document.querySelector('#snsdescp').innerHTML = '€';
如果要使用jQuery,只需调用其.html()
方法:
$('#snsdescp').html('€');
我不明白为什么要存储在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>
您好//假设您的id在这里
var值=”//将php值存储在js变量中
$(“#共享按钮”)。单击(函数(){
$(“#snsdescp”).val(价值);
});
您可以对€\u20AC使用Unicode
$('snsdescp').val(“\u20AC”)
方法.val()
不设置html,因此“&euro;”
不被视为html实体,只被视为普通字符串。您可以使用诸如.val($(“”).html(“&euro;”).text())
之类的“hack”来获取实际的欧元字符。因为您将其作为文本区域
中的值添加,而不是作为将被解析的html添加。可能有帮助,服务器端不需要破解它,使用html()
而不是val()
。比如$('#snsdescp').html('&euro;')代码>只需使用实际的欧元字符€
。确保文件保存为UTF-8,用作UTF-8,并且HTML元字符集设置为UTF-8。textarea没有innerHTML
属性。这一点很好。在这种情况下,Chrome似乎尝试做正确的事情,即使它不受跨平台支持。如果文本包含