用于修复无效html的Javascript



我只是想知道是否有人知道一个javascript工具可以自动修复无效的html语法?我正在开发一个基于javascript的就地编辑器,但我还没有一个优雅的解决方案来防止像这样的糟糕嵌套:

<p><span></p></span>

我想知道是否已经存在一个脚本,它可以接受无效的html并自动返回清理过的html?有没有类似的事情已经存在,或者我必须自己解决这个问题?

是。有W3C验证器。输入您的URI,然后点击More Options,然后选择Clean up markup with HTML-Tidy,瞧。然而,这可能会打乱您正在做的其他事情,所以我建议您自己进行

Pippin关于MarkItUp的评论正是我所需要的。为了将来任何有同样问题的人的参考,我会采用它。

为了将来参考,使用markitup设置自定义编辑器就像创建以下设置哈希一样简单:

var settings = {
  onShiftEnter:     {keepDefault:false, replaceWith:'<br />n'},
  onCtrlEnter:      {keepDefault:false, openWith:'n<p>', closeWith:'</p>'},
  onTab:            {keepDefault:false, replaceWith:'    '},
  markupSet:  [     
    {name:'Bold', key:'B', openWith:'(!(<strong>|!|<b>)!)', closeWith:'(!(</strong>|!|</b>)!)' },
    {name:'Italic', key:'I', openWith:'(!(<em>|!|<i>)!)', closeWith:'(!(</em>|!|</i>)!)'  },
    {name:'Stroke through', key:'S', openWith:'<del>', closeWith:'</del>' },
    {separator:'---------------' },
    {name:'Bulleted List', openWith:'    <li>', closeWith:'</li>', multiline:true, openBlockWith:'<ul>n', closeBlockWith:'n</ul>'},
    {name:'Numeric List', openWith:'    <li>', closeWith:'</li>', multiline:true, openBlockWith:'<ol>n', closeBlockWith:'n</ol>'},
    {separator:'---------------' },
    {name:'Picture', key:'P', replaceWith:'<img src="[![Source:!:http://]!]" alt="[![Alternative text]!]" />' },
    {name:'Link', key:'L', openWith:'<a href="[![Link:!:http://]!]"(!( title="[![Title]!]")!)>', closeWith:'</a>', placeHolder:'Your text to link...' },
    {separator:'---------------' },
    {name:'Clean', className:'clean', replaceWith:function(markitup) { return markitup.selection.replace(/<(.*?)>/g, "") } },       
    {name:'Preview', className:'preview',  call:'preview'}
  ]
}

然后只需包含插件脚本及其相关的样式表,并在onDomReady:中添加这行javascript

$("#markItUp").markItUp(settings);

最新更新