Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javascript或jquery中更改outerHTML_Javascript_Jquery - Fatal编程技术网

在javascript或jquery中更改outerHTML

在javascript或jquery中更改outerHTML,javascript,jquery,Javascript,Jquery,我想更改outerHTML,这是我要做的: $(data.description)[0].outerHTML = $(data.description)[0].outerHTML.replace('red', 'black'); 该值为: $(data.description)[0].outerHTML: "<p style="color: red;">desc1</p>" $(data.description)[0]。outerHTML:“desc1” 而且它不会

我想更改outerHTML,这是我要做的:

$(data.description)[0].outerHTML = $(data.description)[0].outerHTML.replace('red', 'black');
该值为:

$(data.description)[0].outerHTML: "<p style="color: red;">desc1</p>"
$(data.description)[0]。outerHTML:“

desc1

而且它不会被改变。如何改变它

data.description具有以下值:

<p style="color: red;">desc1</p><p style="color: red;">desc2</p>
描述1

我只想更改$(data.description)[0]

这是我的全部代码:

var ingsLines = $(data.description);
for (var i =0; i < ingsLines.length; i++) {
    if (someCondition) {
        $(data.description).eq(i).css('color', 'black');
    }
}
try{$('#myTextarea').html(data.description);}catch(err){}
var-ingsLines=$(data.description);
对于(变量i=0;i
代码会更改值,将黑色替换为红色,但data.description将保留为红色。

使用

$(data.description).replaceWith(function(){
    return this.outerHTML.replace('red', 'black')
});

如果只想更改第一个,可以使用以下选项:

$(data.description).eq(0).replaceWith(function(){
    return this.outerHTML.replace('red', 'black')
});

如果要更改第一个元素的颜色属性,可以使用
css
eq
方法:

$(data.description).eq(0).css('color', 'black');

不是,它是只读的。改为使用jQuery的
replaceWith
。我想OP希望更改所有出现的内容,而不仅仅是样式中的颜色。@Srcee这取决于是否附加标记,这不会更改数据对象的属性,是否可以提供演示?好的,请再次查看我编辑的问题。您的代码更改了颜色,但仍然不起作用-我的textarea中的文本保持红色。@Srcee textarea中文本的颜色保持红色,因为您无法向其附加元素,它只接受文本内容。那么如何将元素转换为文本?@dystroy但您的代码更改了整个数据的颜色。说明,我希望它只更改带有[I]索引的行,然后我是否需要以某种方式调用此replaceWith方法?好的,请再次查看我编辑的问题。您的代码更改了颜色,但仍然不起作用-我的文本区域中的文本保持红色。