有点不清楚您要达到的目标。但是,您似乎正在尝试从没有字体的区域获取字体。不间断空格 (
) 在字体标签 ( <FONT> . . . </FONT>
) 之外,因此没有该标签的任何属性(面或大小)。如果非中断空间在字体标签内,你可以得到它的脸。
这是一个说明这一点的小提琴。为了看到一些东西,我 
用Hello
.
HTML:
<!-- Hello is outside the font tag. -->
<P><FONT size=5 face="Courier New"></FONT>Hello</P>
<!-- Hello is inside the font tag. -->
<p><font size=5 face="Times New Roman">Hello</font><p>
Javascript:
// Alert the face
function handleFonts(e) {
alert(this.face);
}
// Get all the font elements
var el = document.querySelectorAll("font");
// Bind event handlers to the elements
// The last element of "el" is it's length so we only
// iterate to el.length - 1 or i < el.length
for (var i = 0; i < el.length; i++) {
el[i].addEventListener("click", handleFonts, true);
el[i].addEventListener("keypress", handleFonts, true);
}
单击第一段标签中的文本不会触发任何内容。单击第二个中的文本可以正常工作。
我们可以通过一些额外的 javascript 来解决这个问题。
使用第一个标签中的 HTML 和下面的 Javascript,我们可以在包含 的标签内获取字体的外观 
,即使 
不在该字体标签内。
HTML:
<p id="last-p-tag"><font size=5 face="Tahoma"></font>Hello</p>
Javascript:
// Get the paragraph tag we want
var lastPTag = document.getElementById("last-p-tag");
// Bind an event to clicking on it
lastPTag.addEventListener("click", function(e) {
// Alert the face attribute of the first font element
// within that p tag
alert(this.querySelector("font").face);
}, true);
这包含在小提琴的末尾。