我正好有这个问题。我的跨度元素消失了。而如果我使用 div 我可以看到它们。但我当然不想要 div 元素,因为它会导致换行。
该死的!该死的爪哇。
编辑!
停止新闻!
找到了答案。至少,一个为我解决问题的答案。
我仍然能够确定我有我的 span 元素。我将描述我在做什么,并提供代码来说明我是如何做到的。
我想知道插入符号在哪个元素中。因此,此代码存在于 caretUpdate 函数中,它每次移动时都会为我提供插入符号的位置。
@Override
public void caretUpdate(CaretEvent e)
{
System.out.println("caret event: " + e.toString());
Object source = e.getSource();
if (source instanceof JEditorPane)
{
JEditorPane jep = (JEditorPane)source;
Document doc = jep.getDocument();
if (doc instanceof HTMLDocument)
{
HTMLDocument hdoc = (HTMLDocument)doc;
int pos = e.getDot();
Element elem = hdoc.getCharacterElement(pos);
AttributeSet a = elem.getAttributes();
AttributeSet spanAttributeSet = (AttributeSet)a.getAttribute(HTML.Tag.SPAN);
// if spanAttributeSet is not null, then we properly found ' a span '.
// now we need to discover if it is one of OUR spans
if (spanAttributeSet!=null)
{
Object type = spanAttributeSet.getAttribute(HTML.Attribute.TYPE);
if (type !=null && type.equals("dragObject"))
{
// for our logging, we get the ref, which holds the source
// of our value later
System.out.println("the value is: " + spanAttributeSet.getAttribute("ref"));
}
}
}
}
}
编辑!!!
从头开始...这几乎可以工作...除了 Sun 的白痴决定密钥将是 HTML.Attribute 类型。不仅如此,HTML.Attribute 的构造函数是私有的,而且恰好我想要的属性类型不存在于他们的特权属性集中。混蛋!所以,一切都没有丢失......我仍然可以通过枚举器得到它......但这比它需要的要困难一些。
最后编辑!
好的,我现在明白了。如果属性是已知类型,则将其作为 HTML.Attribute("type") 的实例存储在 AttributeSet 中。否则,它以“String”作为键存储在 AttributeSet 中。愚蠢的。但我已经到了。