简体   繁体   English

JavaScript字符串中的特殊字符

[英]Special characters in javascript string

Using Firebug I have a TypeError - slice is not a function. 使用Firebug我有一个TypeError-slice不是函数。 I think this is because I am trying to use slice on a number, although I thought it would be a string. 我认为这是因为我试图在数字上使用切片,尽管我认为这将是字符串。

The string in question is "****01814c6538032b****" and this comes into the page via an ajax command but for testing at the moment I just declare it. 有问题的字符串是"****01814c6538032b****" ,它通过ajax命令进入页面,但出于测试目的,我只声明了它。

hexString = "****01814c6538032b****"

....and now to my question at last! ....现在我的问题终于到了! Is there a way to explicitly declare this as a string? 有没有办法将其明确声明为字符串? I could use toString to change it but wondered if I could specify it as a string in the first place and make sure it will stay one. 我可以使用toString进行更改,但想知道是否可以首先将其指定为字符串并确保它保持原样。

** edit ** Here is all the javascript code, my bit of code starts from hexString = . **编辑**这是所有的javascript代码,我的部分代码从hexString =开始。

<script type="text/javascript">

<!--
// Parses the xmlResponse from status.xml and updates the status box
function updateStatus(xmlData) {
    var mainstat = document.getElementById('display').style.display;
    var loadstat = document.getElementById('loading').style.display;

    // Check if a timeout occurred
    if(!xmlData)
    {
        mainstat = 'none';
        loadstat = 'inline';    

        return;
    }

    // Make sure we're displaying the status display
    mainstat = 'inline';
    loadstat = 'none';

    // Loop over all the LEDs
    for(i = 0; i < 5; i++)
        document.getElementById('led' + i).style.color = (getXMLValue(xmlData, 'led' + i) == '1') ? '#ff8b00' : '#777';

    // Loop over all the buttons
    for(i = 0; i < 5; i++)
        document.getElementById('btn' + i).innerHTML = (getXMLValue(xmlData, 'btn' + i) == 'up') ? '&Lambda;' : 'V';

    // Update the POT value
    for(i = 0; i < 2; i++)
        document.getElementById('pot'+i).innerHTML = getXMLValue(xmlData, 'pot'+i);

    // Update for bargraph
    for(i = 1; i < 3; i++)
    {
        var wd=0;
        wd= (getXMLValue(xmlData, 'pot'+(i-1)))/5;
        document.getElementById('bar'+i).style.width=wd+"px";
    }


    // Update string value
    document.getElementById('test').innerHTML =
    getXMLValue(xmlData, 'test');




var hexString="****01814c6538032b****";

s1 = hexString.slice(0,4);
document.getElementById("slice1").innerHTML=s1;
s2 = hexString.slice(4,6);
document.getElementById("slice2").innerHTML=h2d(s2);
s3 = hexString.slice(6,10);
document.getElementById("slice3").innerHTML=h2d(s3);
s4 = hexString.slice(10,14);
document.getElementById("slice4").innerHTML=h2d(s4);
s5 = hexString.slice(14,18);
document.getElementById("slice5").innerHTML=h2d(s5);
s6 = hexString.slice(18,22);
document.getElementById("slice6").innerHTML=s6;

tempString = h2d(s2);
sensorNo = parseInt(tempString); 

tempString = h2d(s3);
humVal = parseInt(tempString);

  HumidityTemp=(125/65536)*humVal;
  HumidityTemp=HumidityTemp-6;

document.getElementById("slice3").innerHTML=HumidityTemp.toString();


tempString = h2d(s4);
tempVal = parseInt(tempString);  

  TemperatureTemp=(175.72/65536)*tempVal;
  TemperatureTemp=TemperatureTemp-46.85;

  document.getElementById("slice4").innerHTML=TemperatureTemp.toString();

tempString = h2d(s5) ;
voltVal = parseInt(tempString);

  VoltageTemp = 2095/voltVal;

  document.getElementById("slice5").innerHTML=VoltageTemp.toString();



}
setTimeout("newAJAXCommand('status.xml', updateStatus, true)",500);


function h2d(h) 
{
return parseInt(h,16);

}



</script>

You can convert numbers to strings just by adding an empty string to it, like so: 您只需将数字添加为空字符串即可将数字转换为字符串,如下所示:

var str = 1+""; // === "1"

Also, this could be related. 另外, 可能是相关的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM