Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/364.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在HTML元素中查找和替换样式属性_Javascript_Html_Replace - Fatal编程技术网

Javascript在HTML元素中查找和替换样式属性

Javascript在HTML元素中查找和替换样式属性,javascript,html,replace,Javascript,Html,Replace,我需要搜索HTML字段并用替换的任何实例 这是我第一次在现场工作。然而,由于这只适用于第一次出现,我需要能够循环通过它们来替换每一个 我应该如何构造循环来完成此任务 注意:我正在使用ServiceNow,因此我需要它来使用不带库的常规JavaScript(如jQuery)。 起始代码如下: var note = current.work_notes; // Get contents of the HTML field in ServiceNow similar to getElementByID

我需要搜索HTML字段并用
替换
的任何实例

这是我第一次在现场工作。然而,由于这只适用于第一次出现,我需要能够循环通过它们来替换每一个

我应该如何构造循环来完成此任务

注意:我正在使用ServiceNow,因此我需要它来使用不带库的常规JavaScript(如jQuery)。
起始代码如下:

var note = current.work_notes; // Get contents of the HTML field in ServiceNow similar to getElementByID
var noteStyle = note.replace("\<img style\=\"", "\<img style\=\"display\: block\; ");  
var note=current.work\u notes;//获取ServiceNow中HTML字段的内容,类似于getElementByID

var noteStyle=note.replace(“\您似乎在执行本地替换而不是全局替换。这意味着您可以从提供的源字符串中找到第一个实例,而全局
replace
将生成要更改的源字符串中的所有实例

要执行此操作,您需要将代码更改为如下所示,以便它包含全局
替换该字符串所需的命令:

var note = current.work_notes; // Get contents of the HTML field in ServiceNow similar to getElementByID
var noteStyle = note.replace(/\<img style\=\"/g, "\<img style\=\"display\: block\; ");  
var note=current.work\u notes;//获取ServiceNow中HTML字段的内容,类似于getElementByID

var noteStyle=note.replace(/\为什么要将所有额外的转义字符(\)放在那里?除了转义
时,您不需要它们。”
你真的应该用DOM而不是文本来做这件事。这会让事情变得更简单。我使用的是ServiceNow,最好的做法是不要使用DOM,因为全局结构经常在系统更新中被修改。有什么原因人们会否决这个问题吗?如果你指定父节点(在本例中我编写了文档):
varimgs=document.querySelectorAll('img');for(vari=0;I