bounty将在5天后过期。回答此问题可获得+50的声望奖励。Karuppiah RK正在寻找此问题的更详细的答案。
我正在尝试将随机类名称添加到包含内联背景色的元素。我想再添加一个条件。如果父元素和子元素都有内联背景色,则无需为子元素添加类名称。需要跳过该特定子元素。如果父元素和子元素都包含背景色,如何跳过为子元素添加类名称?
function modifyContent(content) {
const doc = new DOMParser().parseFromString(content, "text/html");
// select only elements with a style attribute:
for (const element of doc.querySelectorAll('[style]')) {
if (element.style.backgroundColor) {
element.classList.add('someRandomClassName');
}
}
return doc.body.innerHTML;
}
const input = `<div>
<div style="font-size : 16px; font-family : Arial, Helvetica, sans-serif; ">
<div>
<span style="color : rgb(0, 0, 0); font-family : Arial, Helvetica, sans-serif; font-size : 13px; font-style : normal; font-weight : 400; letter-spacing : normal; orphans : 2; text-indent : 0px; text-transform : none; white-space : normal; widows : 2; word-spacing : 0px; background-color : rgb(255, 255, 255); " class="x_965909101highlight"><span style="font-family : tahoma, arial, helvetica, sans-serif, sans-serif; " class="x_965909101font"><span style="font-size : 13px; " class="x_965909101size"><span class="x_-809421787highlight" style="background-color : rgb(255, 153, 204); ">ncern: </span></span></span></span><span style="color : rgb(0, 0, 0); font-style : normal; font-weight : 400; letter-spacing : normal; orphans : 2; text-indent : 0px; text-transform : none; widows : 2; word-spacing : 0px; font-size : 14px; font-family : LatoRegular, sans-serif; white-space : pre-wrap; "><span style="background-color : rgb(255, 255, 255); " class="x_965909101highlight"><span style="font-family : tahoma, arial, helvetica, sans-serif, sans-serif; " class="x_965909101font"><span style="font-size : 13px; " class="x_965909101size"><span class="x_-809421787highlight" style="background-color : rgb(255, 153, 204); ">Description </span></span></span></span></span><br>
</div>
<div><br></div>
<div><span style="color : rgb(0, 0, 0); font-style : normal; font-weight : 400; letter-spacing : normal; orphans : 2; text-indent : 0px; text-transform : none; widows : 2; word-spacing : 0px; font-size : 14px; font-family : LatoRegular, sans-serif; white-space : pre-wrap; "><span style="background-color : rgb(255, 255, 255); " class="x_965909101highlight"><span style="font-family : tahoma, arial, helvetica, sans-serif, sans-serif; " class="x_965909101font"><span style="font-size : 13px; " class="x_965909101size"><span class="x_-809421787highlight" style="background-color : rgb(255, 153, 204); ">
<span style="color : rgb(0, 0, 0); font-family : Arial, Helvetica, sans-serif; font-size : 13px; font-style : normal; font-weight : 400; letter-spacing : normal; orphans : 2; text-indent : 0px; text-transform : none; white-space : normal; widows : 2; word-spacing : 0px; background-color : rgb(255, 255, 255); " class="x_965909101highlight"><span style="font-family : tahoma, arial, helvetica, sans-serif, sans-serif; " class="x_965909101font"><span style="font-size : 13px; " class="x_965909101size">ncern: </span></span></span><span style="color : rgb(0, 0, 0); font-style : normal; font-weight : 400; letter-spacing : normal; orphans : 2; text-indent : 0px; text-transform : none; widows : 2; word-spacing : 0px; font-size : 14px; font-family : LatoRegular, sans-serif; white-space : pre-wrap; "><span style="background-color : rgb(255, 255, 255); " class="x_965909101highlight"><span style="font-family : tahoma, arial, helvetica, sans-serif, sans-serif; " class="x_965909101font"><span style="font-size : 13px; " class="x_965909101size">Description </span></span></span></span>
</span></span></span></span></span><br>
</div>
<div><br></div>
<div><span style="color : rgb(0, 0, 0); font-style : normal; font-weight : 400; letter-spacing : normal; orphans : 2; text-indent : 0px; text-transform : none; widows : 2; word-spacing : 0px; font-size : 14px; font-family : LatoRegular, sans-serif; white-space : pre-wrap; "><span style="background-color : rgb(255, 255, 255); " class="x_965909101highlight"><span style="font-family : tahoma, arial, helvetica, sans-serif, sans-serif; " class="x_965909101font"><span style="font-size : 13px; " class="x_965909101size"><span class="x_-809421787highlight" style="background-color : rgb(254, 255, 102); ">
</span><span style="color : rgb(0, 0, 0); font-family : Arial, Helvetica, sans-serif; font-size : 13px; font-style : normal; font-weight : 400; letter-spacing : normal; orphans : 2; text-indent : 0px; text-transform : none; white-space : normal; widows : 2; word-spacing : 0px; background-color : rgb(255, 255, 255); " class="x_965909101highlight"><span style="font-family : tahoma, arial, helvetica, sans-serif, sans-serif; " class="x_965909101font"><span style="font-size : 13px; " class="x_965909101size"><span class="x_-809421787highlight" style="background-color : rgb(254, 255, 102); ">ncern: </span></span></span></span><span style="color : rgb(0, 0, 0); font-style : normal; font-weight : 400; letter-spacing : normal; orphans : 2; text-indent : 0px; text-transform : none; widows : 2; word-spacing : 0px; font-size : 14px; font-family : LatoRegular, sans-serif; white-space : pre-wrap; "><span style="background-color : rgb(255, 255, 255); " class="x_965909101highlight"><span style="font-family : tahoma, arial, helvetica, sans-serif, sans-serif; " class="x_965909101font"><span style="font-size : 13px; " class="x_965909101size"><span class="x_-809421787highlight" style="background-color : rgb(254, 255, 102); ">Description </span></span></span></span></span><span class="x_-809421787highlight" style="background-color : rgb(255, 153, 204); ">
</span></span></span></span></span><br>
</div>
<div><br></div>
<div><span style="color : rgb(0, 0, 0); font-style : normal; font-weight : 400; letter-spacing : normal; orphans : 2; text-indent : 0px; text-transform : none; widows : 2; word-spacing : 0px; font-size : 14px; font-family : LatoRegular, sans-serif; white-space : pre-wrap; "><span style="background-color : rgb(255, 255, 255); " class="x_965909101highlight"><span style="font-family : tahoma, arial, helvetica, sans-serif, sans-serif; " class="x_965909101font"><span style="font-size : 13px; " class="x_965909101size"><span class="x_-809421787highlight" style="background-color : rgb(255, 153, 204); ">
</span><span style="color : rgb(0, 0, 0); font-family : Arial, Helvetica, sans-serif; font-size : 13px; font-style : normal; font-weight : 400; letter-spacing : normal; orphans : 2; text-indent : 0px; text-transform : none; white-space : normal; widows : 2; word-spacing : 0px; background-color : rgb(255, 255, 255); " class="x_965909101highlight"><span style="font-family : tahoma, arial, helvetica, sans-serif, sans-serif; " class="x_965909101font"><span style="font-size : 13px; " class="x_965909101size"><span class="x_-809421787highlight" style="background-color : rgb(204, 204, 204); ">ncern: </span></span></span></span><span style="color : rgb(0, 0, 0); font-style : normal; font-weight : 400; letter-spacing : normal; orphans : 2; text-indent : 0px; text-transform : none; widows : 2; word-spacing : 0px; font-size : 14px; font-family : LatoRegular, sans-serif; white-space : pre-wrap; "><span style="background-color : rgb(255, 255, 255); " class="x_965909101highlight"><span style="font-family : tahoma, arial, helvetica, sans-serif, sans-serif; " class="x_965909101font"><span style="font-size : 13px; " class="x_965909101size"><span class="x_-809421787highlight" style="background-color : rgb(204, 204, 204); ">Description </span></span></span></span></span><span class="x_-809421787highlight" style="background-color : rgb(255, 153, 204); ">
</span></span></span></span></span><br>
</div>
<div><br></div>
<div><span style="color : rgb(0, 0, 0); font-style : normal; font-weight : 400; letter-spacing : normal; orphans : 2; text-indent : 0px; text-transform : none; widows : 2; word-spacing : 0px; font-size : 14px; font-family : LatoRegular, sans-serif; white-space : pre-wrap; "><span style="background-color : rgb(255, 255, 255); " class="x_965909101highlight"><span style="font-family : tahoma, arial, helvetica, sans-serif, sans-serif; " class="x_965909101font"><span style="font-size : 13px; " class="x_965909101size"><span class="x_-809421787highlight" style="background-color : rgb(255, 153, 204); ">
</span></span></span></span></span>
<br>
</div>
</div>
<br>
</div>`;
console.log(modifyContent(input));
3条答案
按热度按时间gblwokeq1#
你必须这样做:
在parentElement中检查样式backgroundColor是否存在。然后在children集合中检查某些元素是否具有backgroundColor类。
qltillow2#
如果你想跳过向一个元素添加随机类,如果 any 它的子元素有内联背景色,你可以使用递归来循环遍历子元素:
q8l4jmvw3#
I am not 100% sure of what you are asking but here is my take on it. Please note there is only 1
<span>
element in your example that meet your condition, if I understand it correctly (condition being: self and parent havebackground-color
style attribute set).You can verify that with this selector:
doc.querySelectorAll("[style*=\"background-color\"] > [style*=\"background-color\"]")
.Your code becomes:
The span(s) with a green border is/are the skipped element(s).