2012-10-17 36 views
4

$(editor[i])[0].outerHTML的数值为:变化outerHTML在JavaScript

<p style="color: red;" data-mce-style="color: red;">some string</p> 

我想data-mce-style="color: red;"消失。
我做了这样的:

$(editor[i])[0].outerHTML.replace('data-mce-style="color: red;"', ''); 

但它不是取代它。

回答

8

.replace创建一个新转化串;它不会改变原始变量。你只需要创建一个新的字符串,而不是存储新的字符串返回到outerHTML,如:

$(editor[i])[0].outerHTML = $(editor[i])[0].outerHTML.replace('data-mce-style="color: red;"', ''); 

然而,这只是解决了眼前的问题 - 有好得多的方式来完成你需要比字符串化什么,重新解析你的<p>元素。由于您使用jQuery的,最明显的方法是使用removeAttr方法:

$(editor[i]).removeAttr('data-mce-style')​;​ 
+0

这是正确的答案OP的问题,我想。 –

0

尝试使用jQuery removeData()

$(editor[i]).removeData('mce-style'); 
2

尝试:

$(editor[i]).removeAttr('data-mce-style') 

http://api.jquery.com/removeAttr/

当然,这将适用于您选择的所有元素。如果你只是想将其应用到元素0,则使用:

$(editor[i]).first().removeAttr('data-mce-style') 
1
$(editor[i]).removeAttr('data-mce-style')​;​ 

FIDDLE

0

您需要更改/删除特定的属性,你需要使用

$(editor[i])[0].removeAttr('data-mce-style'); 

欲了解更多信息,请查询以下链接: http://api.jquery.com/removeAttr/

如果您需要更改特定属性的值,然后执行:

attr(attributeName , value ); 

更多的信息差不多查看以下链接: http://api.jquery.com/attr/