关于所见即所得编辑器中的字体名称/大小下拉菜单

发布于 2024-12-21 00:45:28 字数 606 浏览 2 评论 0原文

我正在开发 IE 的富文本编辑器,我想问一个有关在当前插入点获取“fontname”值的问题。

问题出在空行上,假设用户在编辑器中输入:

line 1

line 2

空行位于“第 1 行”和“第 2 行”之间,本例中空行的 html 源代码是(当用户按“enter”):

<P><FONT size=5 face="Courier New"></FONT>&nbsp;</P>

问题是这样的: document.queryCommandValue("fontname") 在鼠标单击空行以及将光标移动到空行的情况下给出不同的值使用键盘线路。

如果单击鼠标,它会为我提供浏览器的默认字体名称,而在其他情况下(使用键盘移动光标),它会为我提供正确的字体名称(“Courier New”)。

实际上在这两种情况下,document.selection具有不同的“type”值:鼠标单击时为“text”,键盘时为“none”。

任何帮助将不胜感激!

如果我的问题不清楚,请告诉我。

I'm working on a Rich Text Editor for IE, and I would like to ask a question about getting "fontname" value at the current insertion point.

The issue is with empty lines, let's say in the editor the user has typed in:

line 1

line 2

The empty line is between "line 1" and "line 2", the html source of the empty line in this example is (generated by IE when the user press "enter"):

<P><FONT size=5 face="Courier New"></FONT> </P>

and the problem is this: document.queryCommandValue("fontname") gives me different values in case of mouse click the empty line, and in case of moving cursor to the empty line using keyboard.

In case of mouse click, it gives me the default font name of the browser, while in the other case (move cursor using keyboard) it gives me the correct fontname ("Courier New").

Actually in these two cases, document.selection has different "type" values: "text" when mouse click and "none" when keyboard.

Any help will be much appreciated!

Please kindly let me know if my question is not clear.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执手闯天涯 2024-12-28 00:45:28

目前尚不清楚您想要实现什么目标。然而,您似乎正在尝试从没有字体的区域获取字体。不间断空格 ( ) 位于字体标记 (. . .) 之外,因此没有任何属性该标签(面孔或尺寸)。如果不间断空格位于字体标签内,您就可以获得它的外观。

这是一个 fiddle 来说明这一点。为了看到一些东西,我用 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);

这包含在小提琴的末尾。

It's somewhat unclear what you're trying to achieve. However it looks like you are trying to get the font face from an area that has none. The non breaking space ( ) is outside the font tag (<FONT> . . . </FONT>) and therefore has none of the attributes of that tag (face or size). If the non breaking space were inside the font tag, you could get its face.

Here's a fiddle illustrating that. For the sake of seeing something, I replaced   with 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);
}

Clicking on the text in the first paragraph tag triggers nothing. Clicking on the text in the second works fine.

We can get around this though with a little extra javascript.

With HTML like in the first tag and the following Javascript, we can get the face of the font within the tag containing  , even though   is not within that font tag.

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);

This is included at the end of the fiddle.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文