In WordPress, <img>
is inside <a>
tag like below.
<a>link text</a>
<a><img src="#"></a>
I added style of dotted border to the bottom of <a>
, but there’s also dotted border under every picture I post.
So I tried to add a function in function.php, in order to remove border of <a><img src="#"></a>
, but it failed. Please help me with code below.
function removeAnchorBorder() {
var anchorWithPic = document.getElementsByTagName("a");
if (anchorWithPic.contains(img) = true) {
anchorWithPic.style.border = "none";
}
add_filter("the_content", "removeAnchorBorder");
Since CSS doesn’t have look-ahead, you can’t override it with plain CSS and must resort to using JavaScript.
This is a very naive approach, with much logic that could be optimized or abstracted to use jQuery if you prefer:
Here is a visual example.
Also note that
.forEach()
may not be available in all browsers, and theArray slice
reference is just a trick to convert the selected NodeList into actual iterable arrays.