我已经成功实现了一些代码,它使用stripTags() 从粘贴的字符串中删除所有HTML。我的下一个目标是用白旗标记一些标签,以便在使用 .wrap() 来增强功能的“粘贴”事件中忽略它们。
我正在使用prototype.js 作为框架,并且一直在慢慢地经历学习框架和javascript 的成长痛苦,但是这个问题出现了一些障碍。
我用谷歌搜索了一下,发现了两个很棒的解决方案,但我似乎没有正确实施它们。 找到的解决方案: http: //perfectionkills.com/wrap-it-up/(指示要删除的标签的功能)和 http://pastebin.com/xbymCFi9(允许保留标签的功能)
我几乎从后者复制和粘贴。如果我从代码中提取“br”,那么正则表达式将被忽略,并且所有 html 都会被剥离。如果我离开它,则不会粘贴任何内容。
这是我拼凑的内容(我因为无法弄清楚这一点而感到愚蠢!)。
String.prototype.stripTags = String.prototype.stripTags.wrap(
function(proceed, allowTags) {
if (allowTags) {
if (Object.isString(allowTags)) allowTags = $w(allowTags)
this.gsub(/(<\/?\s*)([^\s>]+)(\s[^>]*)?>/, function(match) {
if (allowTags.include(match[2].toLowerCase()))
return match[1] + match[2] + match[3] + '>'
})
} else {
// proceed using the original function
return proceed();
}
});
WysiHat.Commands.promptLinkSelection = function() {
if (this.linkSelected()) {
if (confirm("Remove link?"))
this.unlinkSelection();
} else {
var value = prompt("Enter a URL", "http://www.alltrips.com/");
if (value)
this.linkSelection(value);
}
}
document.on("dom:loaded", function() {
var editor = WysiHat.Editor.attach('event_desc');
var toolbar = new WysiHat.Toolbar(editor);
editor.observe("paste", function(event) {
var el = $(this);
setTimeout(function() {
var pText = el.innerHTML.stripTags('br');
//alert(pText);
$('event_desc_editor').update(pText);
$('event_desc').setValue(pText);
}, 0);
});
(您可能会从 37Signals 文本编辑器中识别出 WysiHat 代码)
注意:您可以看到已注释掉的警报。如果我提醒 ptext,我会返回“未定义”。