0

一个奇怪的问题。很难用几句话来形容,但我会努力的。(我是 JavaScript 初学者)。

当鼠标悬停在.u标签上时,我希望它做一些事情。所以我有:

var dotUElements = document.getElementsByClassName("u");

for (var ii = 0; ii < dotUElements.length; ii++) {
    dotUElements[ii].onmouseover = createHandler1( dotUElements[ii] );
    dotUElements[ii].onmouseout = createHandler2( dotUElements[ii] );
}

然后添加处理程序:

function createHandler1( myobj ) {
    return (function() {ff1(myobj);});
}

function createHandler2( myobj ) {
    return (function() {ff2(myobj);});
}

我的鼠标和鼠标功能ff1是:mouseoverff2

function ff1 (myobj) {
    myobj.style.color="red";
    var uChar = myobj.firstChild.nodeValue;
    var uNum =  uChar.charCodeAt(0);
    uName = unicodeTable(uNum);
    var uHex = (uNum).toSring(16);
}

function ff2 (myobj) {
    myobj.style.color="black";
}

问题是最后一行

    var uHex = (uNum).toSring(16);

它会产生错误:

错误:TypeError:uNum.toSring 不是函数

如果我简单地替换该行

(30).toSring(16);

同样的错误。关于出了什么问题的任何提示?我有代码

http://xahlee.info/js/ex/xx_test.html

4

1 回答 1

-1

这是您的示例的工作演示

function unicodeTable(num) {
  var unicodedata = {
    0x2190: "LEFTWARDS ARROW",
    0x2191: "UPWARDS ARROW",
    0x2192: "RIGHTWARDS ARROW",
    0x2193: "DOWNWARDS ARROW",
  };
  return unicodedata[num];
}

function createHandler1(myobj) {
  return (function() {
    ff1(myobj);
  });
}

function createHandler2(myobj) {
  return (function() {
    ff2(myobj);
  });
}

function ff1(myobj) {
  myobj.style.color = "red"; // test code. so we know it's activated
  var uChar = myobj.firstChild.nodeValue;
  var uNum = uChar.charCodeAt(0);
  uName = unicodeTable(uNum);
  var uHex = (uNum).toString(16);
  alert(uHex);
}

function ff2(myobj) {
  myobj.style.color = "black"; // test code. so we know it's activated
}

var dotUElements = document.getElementsByClassName("u");

for (var ii = 0; ii < dotUElements.length; ii++) {
  dotUElements[ii].onmouseover = createHandler1(dotUElements[ii]);
  dotUElements[ii].onmouseout = createHandler2(dotUElements[ii]);
}
<body>

  <h1>unicode test 3</h1>

  <p>
    <b class="u">←&lt;/b>
    <b class="u">→&lt;/b>
    <b class="u">↑&lt;/b>
    <b class="u">↓&lt;/b>
  </p>

  <p>when mouse hover on b.u element, show the unicode name.</p>
</body>

我添加了一个alert(uHex);来显示价值。

让我知道它是否不起作用。

于 2013-03-02T22:38:19.157 回答