Javascript JQuery替换标签 [英] Javascript JQuery replace tags

查看:82
本文介绍了Javascript JQuery替换标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个满意的区域,当用户完成编辑后,我会将数据保存到文件中。当用户使用第一个浏览器然后另一个浏览器时,由contenteditables创建的不同样式会导致代码混乱且不兼容。

I have a contenteditable area and when the user has finished editing I will save the data to a file. When a user uses first one browser and then another the different styles created by the contenteditables leads to messy and incompatible code.

我想知道是否有任何方法可以替换< span style =font-weight:bold> XXX< / span> 在Chrome中使用'标准'标签创建的标签,例如< ; b> xxx< / b>

I was wondering if there was any way to replace the <span style="font-weight:bold">XXX</span> tags created in Chrome with 'standard' tags such as <b>xxx</b>.

提前致谢

推荐答案

你可以试试这个:

$('span').each(function(){ // iterate to the all the spans
    if($(this).css('font-weight') == 'bold'){  // check if font is bold
       $(this).contents().unwrap().wrap('<b></b>');​ 
    }  // unwrap the content and wrap it
});

那么,这里发生的是:


  1. 遍历文档中的所有跨度

  2. 检查css样式是否为粗体

  3. 如果为true然后首先解开它所持有的内容并用你选择的标签包装

所以类似地你必须检查所有其他的CSS样式分开并相应地更换/包装。

so similarly you have to check for all other css styles separately and replace/wrap it accordingly.

这篇关于Javascript JQuery替换标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆