javascript 在javascript中更改outerHTML

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12934793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 17:28:53  来源:igfitidea点击:

Change outerHTML in javascript

javascriptjquery

提问by petko_stankoski

$(editor[i])[0].outerHTMLhas a value of:

$(editor[i])[0].outerHTML具有以下值:

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

I want data-mce-style="color: red;"to disappear.
I'm doing that like this:

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

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

But it's not replacing it.

但它并没有取代它。

回答by apsillers

.replacecreates a newtransformed string; it does not alter the original variable. You're simply creating a new string and not storing the new string back into outerHTML, like:

.replace创建一个新的转换字符串;它不会改变原始变量。您只是创建一个新字符串,而不是将新字符串存储回outerHTML,例如:

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

However, this only solves your immediate problem -- there are vastly better ways to accomplish what you need than stringifying and re-parsing your <p>element. Since you're using jQuery, the most obvious way would be to use the removeAttrmethod:

但是,这只能解决您当前的问题——有比字符串化和重新解析<p>元素更好的方法来完成您的需求。由于您使用的是 jQuery,最明显的方法是使用该removeAttr方法:

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

回答by James Gaunt

Try:

尝试:

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

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

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

Of course this will apply to all elements in your selector. If you just want to apply this to element 0 then use:

当然,这将适用于选择器中的所有元素。如果您只想将此应用于元素 0,请使用:

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

回答by adeneo

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

FIDDLE

小提琴

回答by Tiberiu-Ionu? Stan

element.setAttribute(attr, null)or element.removeAttribute

element.setAttribute(attr, null)element.removeAttribute

No need for outerHTML and replace. Note that replacing HTML will remove event listeners (other than attribute event handlers).

不需要outerHTML 和replace。请注意,替换 HTML 将删除事件侦听器(属性事件处理程序除外)。

回答by Florent

Try to use jQuery removeData():

尝试使用 jQuery removeData()

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

回答by Abhishek Jain

you need to change/remove a particular attribute , for that you need to use

您需要更改/删除特定属性,为此您需要使用

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

for more info check the following links: http://api.jquery.com/removeAttr/

有关更多信息,请查看以下链接:http: //api.jquery.com/removeAttr/

if you need to change the value of a particular attribute then do:

如果您需要更改特定属性的值,请执行以下操作:

attr( attributeName , value  );

for more info about the same check the following link: http://api.jquery.com/attr/

有关相同检查的更多信息,请查看以下链接:http: //api.jquery.com/attr/