我正在为自己的用法开发一个插件,并且我想摆脱出现在顶部的所有恼人的wordpress消息,包括更新,错误,信息等。鉴于显而易见的事实该插件的菜单页面是由PHP生成,我试图消除与根据我的研究,一个PHP代码,这些消息是这样的:删除父母和所有子元素不工作
function remove_core_updates(){
global $wp_version;return(object) array('last_checked'=> time(),'version_checked'=> $wp_version,);
}
add_filter('pre_site_transient_update_core','remove_core_updates');
add_filter('pre_site_transient_update_plugins','remove_core_updates');
add_filter('pre_site_transient_update_themes','remove_core_updates');
我说这个我插件的PHP文件的顶部,应删除通知,但仍然出现一些插件的错误消息。除此之外,上面的代码将删除我在仪表板中访问的所有菜单页的更新通知。这我不想要。
作为一种变通方法,我写了一个函数,它做什么,我想:
function remove_wp_messages(selector) {
var child = document.querySelector(selector),
parent = child.parentNode,
index = Array.prototype.indexOf.call(parent.children, child);
for (var i = 1; i < Number(index + 1); i++) {
var elements = document.querySelector("#wpbody-content > :nth-child(" + i + ")");
while (elements.firstChild) {
elements.removeChild(elements.firstChild);
elements.style.display = "none";
}
}
}
window.onload = remove_wp_messages("#wpbody-content > link");
鉴于第一个HTML元素我添加到菜单页面是link
到外部样式表,在javascript函数在returns
以上的那个link
的索引并且从元素直到link
,因此循环移除所有子节点。问题在于,我不仅要删除子元素,还要删除父母,这相当于删除我的插件内容以上的通知。我试图取代:
elements.style.display = "none";
有了这个:
elements.remove();
没有运气。我不明白的是,父元素被隐藏起来,但我用remove()
替换它,它删除了我的内容而不是通知。为什么?我打开php和javascript的建议。
SOLUTION
据@PetrSr答案,我的代码现在看起来像这样:
function remove_wp_messages(selector) {
var child = document.querySelector(selector),
parent = child.parentNode,
index = Array.prototype.indexOf.call(parent.children, child);
for (var i = Number(index); i > 0; i--) {
document.querySelector("#wpbody-content > :nth-child(" + i + ")").remove();
}
}
非常简单。
这个技巧!我的做法完全不合逻辑。 –
'Number(index)'必须为'Number(index);'后面使用逗号而不是分号; –