nextElementSibling
是 JavaScript 的一个 DOM 属性,用于获取当前元素的下一个同级元素节点(忽略文本节点和注释节点)。
在 WordPress 中 nextElementSibling
可能不起作用的常见原因:
document.addEventListener('DOMContentLoaded', function() {
// 你的代码
var element = document.querySelector('.your-element');
var nextSibling = element.nextElementSibling;
console.log(nextSibling);
});
jQuery(document).ready(function($) {
var nextSibling = $('.your-element')[0].nextElementSibling;
// 或者使用 jQuery 方法
var nextSiblingJQ = $('.your-element').next();
});
// 对于动态加载的内容,可能需要使用 MutationObserver
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
var element = document.querySelector('.your-element');
if (element) {
var nextSibling = element.nextElementSibling;
console.log(nextSibling);
observer.disconnect(); // 停止观察
}
});
});
observer.observe(document.body, {
childList: true,
subtree: true
});
function getNextElementSibling(element) {
var sibling = element.nextSibling;
while (sibling && sibling.nodeType !== 1) {
sibling = sibling.nextSibling;
}
return sibling;
}
// 使用
var element = document.querySelector('.your-element');
var nextElement = getNextElementSibling(element);
nextElementSibling
通常用于:
通过以上方法,应该能够解决 nextElementSibling
在 WordPress 中不起作用的问题。
没有搜到相关的文章