javascript - 删除 WordPress 中特定标签的样式 (Javascript)

标签 javascript html wordpress contains getelementsbytagname

在 WordPress 中,<img>在里面<a>标签如下。

<a>link text</a>
<a><img src="#"></a>

我在 <a> 的底部添加了虚线边框样式,但我发布的每张图片下面也有虚线边框。

所以我尝试在function.php中添加一个函数,以删除<a><img src="#"></a>的边框,但失败了。请帮我编写下面的代码。

function removeAnchorBorder() {
  var anchorWithPic = document.getElementsByTagName("a");
  if (anchorWithPic.contains(img) = true) {
    anchorWithPic.style.border = "none";
}
add_filter("the_content", "removeAnchorBorder");

最佳答案

CSS doesn't have look-ahead ,您无法使用纯 CSS 覆盖它,而必须使用 JavaScript。

这是一种非常幼稚的方法,如果您愿意,可以优化或抽象许多逻辑以使用 jQuery:

var withoutImg = Array.prototype.slice.call(document.querySelectorAll('a'),0); 
var withImg = Array.prototype.slice.call(document.querySelectorAll('a img'),0);

withoutImg.forEach(function(node){ 
  node.style.borderStyle = "dotted";
});

withImg.forEach(function(node){ 
  node.parentElement.style.borderStyle = "none";
});

Here是一个视觉示例。

另请注意,.forEach() 可能并非在所有浏览器中都可用,并且 Array slice 引用只是将所选 NodeList 转换为实际可迭代数组的技巧.

关于javascript - 删除 WordPress 中特定标签的样式 (Javascript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31768773/

相关文章:

javascript - 仅包含特定数字的 HTML 输入框

javascript - Angular 和 jQuery : Events not attached to dynamically created HTML elements

html - 将元素符号点向右而不是向左对齐

php - WordPress:如何获取标题图像 ID

wordpress - 具有分页关系数据的 Redux 状态

javascript - 为什么 Angular UI Bootstrap 使用提供程序来处理模式和工具提示中的逻辑,而不是其他组件中的逻辑?

javascript - 在 ReactJS 中更改 URL onClick

html - IE8 CSS 旋转

javascript - 如何将带有按钮的图像上传到 Canvas 中

mysql - 如何在已准备好具有多个联接的数据库上执行之间查询?