问题:如何获取输入到如下所述创建的文本框中的字符串中的换行符?
动机:我正在尝试对 FireFox 扩展 (Zotero) 进行一些非常简单的修改,以使其更易于使用。此扩展允许您通过将标签键入文本框并按回车键来为项目输入标签。我希望能够一次输入一堆标签,方法是将它们作为字符串粘贴到文本框中,并将换行符作为标签之间的分隔符。选择换行符是因为其他字符也是用于标签本身的可行选择。
问题:当我现在将一个字符串粘贴到输入文本框中时,它会删除所有换行符并用单个空格替换它们。我在扩展代码中找不到发生这种情况的任何地方,所以我认为这是文本框本身的属性。我正在尝试查找此文本框的文档,看看是否有一些我可以使用的属性,以便我可以捕捉换行符,但我对 JavaScript 和 HTML 不是很熟悉,并且找不到任何东西。我想找到侵入性最小的解决方案(一种不会改变文本框当前用于输入新条目和按回车键的方式的解决方案),以便可以将此修改应用到扩展的核心版本中。
资源:到目前为止,我在网上找到的唯一讨论类似问题的是 this other question关于允许在文本框中使用制表符但不允许换行。我不确定这是 HTML 问题(是否有可以设置为允许保留换行符的属性?)或 JavaScript 问题(我需要重写下面引用的代码吗?)。我一直在尝试查看两者的文档,但到目前为止还没有找到任何东西(大多数 HTML 教程讨论的是文本区域而不是文本框)。
代码:这是创建文本框对象的代码(或者它是一种方法还是一种形式?)我正在处理:
<method name="showEditor">
<parameter name="elem"/>
<body>
<![CDATA[
// Blur any active fields
/*
if (this._dynamicFields) {
this._dynamicFields.focus();
}
*/
Zotero.debug('Showing editor');
var fieldName = 'tag';
var tabindex = elem.getAttribute('ztabindex');
var tagID = elem.parentNode.getAttribute('id').split('-')[1];
var value = tagID ? Zotero.Tags.getName(tagID) : '';
var itemID = Zotero.getAncestorByTagName(elem, 'tagsbox').item.id;
var t = document.createElement("textbox");
t.setAttribute('value', value);
t.setAttribute('fieldname', fieldName);
t.setAttribute('ztabindex', tabindex);
t.setAttribute('flex', '1');
// Add auto-complete
t.setAttribute('type', 'autocomplete');
t.setAttribute('autocompletesearch', 'zotero');
var suffix = itemID ? itemID : '';
t.setAttribute('autocompletesearchparam', fieldName + '/' + suffix);
var box = elem.parentNode;
box.replaceChild(t, elem);
// Prevent error when clicking between a changed field
// and another -- there's probably a better way
if (!t.select) {
return;
}
t.select();
t.addEventListener('blur', function () {
document.getBindingParent(this).blurHandler(this);
}, false);
t.setAttribute('onkeypress', "return document.getBindingParent(this).handleKeyPress(event)");
this._tabDirection = false;
this._lastTabIndex = tabindex;
return t;
]]>
</body>
</method>
最佳答案
这个问题其实是关于XUL,而不是HTML。可以在此处找到文本框对象的文档:
https://developer.mozilla.org/en/XUL/textbox
需要设置的属性是换行符。通过将其设置为“pasteintact”,可以保留换行符。
关于javascript - 在 XUL 的文本框中捕获换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7909676/