当鼠标悬停在.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用于鼠标悬停,ff2用于鼠标输出:
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);
它会产生错误:
Error: TypeError: uNum.toSring is not a function
如果我只用
替换那一行(30).toSring(16);
同样的错误。什么是错的任何暗示?我有代码 http://xahlee.info/js/ex/xx_test.html
答案 0 :(得分:-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">←</b>
<b class="u">→</b>
<b class="u">↑</b>
<b class="u">↓</b>
</p>
<p>when mouse hover on b.u element, show the unicode name.</p>
</body>
我添加了alert(uHex);
来显示值。
如果它不起作用,请告诉我。