前端需求实现,表单对比高亮展示
前置条件:两个不同接口的数据对比,由后端返回有变化的字段数组,前端进行对应字段的高亮展示。
本人解决方案:采用js获取对应dom,对表单进行遍历,将后端返回值对应上字段的差异化进行展示。
最终实现代码如下:
//是否高亮展示
const isHighLight = () => {
const inputForm = document
.querySelector("当前表单页的标识")?.getElementsByTagName("input");
diffFields?.forEach((item: string) => {
//HTMLSelectElement.namedItem()返回指定名称匹配的html
const inputFormItem = inputForm!.namedItem(item);
// 选择框处理
if (lastOpen) {
if (inputFormItem!.getAttribute("type") === "search") {
const selectFormItem: any =
inputFormItem!.parentElement!.nextElementSibling;
selectFormItem!.style.color = "red";
}
inputFormItem!.style.color = "red";
} else {
if