Bar*_*der 0 html javascript jquery
目前我有:
this.html(this.html().replace(/<\/?([i-z]+)[^>]*>/gi, function(match, tag) {
return (tag === 'p') ? match : '<p>';
return (tag === '/p') ? match : '</p>';
return (tag === 'script') ? match : 'script';
return (tag === '/script') ? match : '/script';
}));
Run Code Online (Sandbox Code Playgroud)
但是,<p>和<script>标签仍然被删除,我做错了什么?
您不能将多个return语句与这样的三元运算符一起使用.第一个将被评估,其余的将被忽略.使用适当的if陈述或switch陈述,
if (tag === 'p')
return '<p>';
else if (tag === '/p')
return '</p>';
else if (tag === 'script')
return 'script';
else if (tag === '/script')
return '/script';
else
return match;
Run Code Online (Sandbox Code Playgroud)
switch 例:
switch (tag) {
case 'p': return '<p>';
case '/p': return '</p>';
//...
case default: return match;
}
Run Code Online (Sandbox Code Playgroud)
您还可以将对象用作地图,
var map { 'p': '<p>', '/p' : '</p>' /*, ... */ };
return map[tag] || match;
Run Code Online (Sandbox Code Playgroud)
或嵌套的三元运算符,
return tag === 'p' ? '<p>'
: tag === '/p' ? '</p>'
: tag === 'script' ? '<script>'
: tag === '/script' ? '</script>'
: match;
Run Code Online (Sandbox Code Playgroud)
但这些通常不太可读,难以维护.
| 归档时间: |
|
| 查看次数: |
131 次 |
| 最近记录: |