javascript - 如何将 JS 应用于(打开的) Accordion ,当它在 DOM 加载时关闭

标签 javascript wordpress accordion

关于 https://www.apis.de/dienstleistungen/trainings/我有几个 Accordion 。其中有包含日期​​和其他信息的表格。每个都有一个 ID 生成的服务器端 (php)。我想简单地使用选择器路径(开发工具)获取 tbody 标签 --> 获取子节点并切换,例如node[11] 和 node[12] 左右。问题是我写的 JS 函数没有被识别,因为 Accordion 在页面加载时关闭并且不识别选择器路径(使用 IIFE)

(function(){
let first_table = document.querySelector("#site-wrapper > div > div > main > article > section > ul > li.is-expanded > ul > li > table > tbody"); // gets the tbody of first accordion (Schulungen Deutschland)
console.log(first_table); // returns null, because the accordion was not open when the function is executed. 

})();


最佳答案

编辑:我之前写的不正确。

您可以使用 document.getElementsByTagName('tbody')[0]

访问第一个 tbody 而不是使用查询选择器

如果您能够在客户端访问要交换的元素的 ID,则可以使用 jfriend00 的代码交换它们写道:

function doSwap() {
    swapElements(document.getElementById("one"), document.getElementById("two"));
}

function swapElements(obj1, obj2) {
    // create marker element and insert it where obj1 is
    var temp = document.createElement("div");
    obj1.parentNode.insertBefore(temp, obj1);

    // move obj1 to right before obj2
    obj2.parentNode.insertBefore(obj1, obj2);

    // move obj2 to right before where obj1 used to be
    temp.parentNode.insertBefore(obj2, temp);

    // remove temporary marker node
    temp.parentNode.removeChild(temp);
}
<button onclick="doSwap()">Swap</button><br><br>

<ul>
    <li id="one">Item A</li>
    <li id="two">Item B</li>
    <li>Item C</li>
</ul>

关于javascript - 如何将 JS 应用于(打开的) Accordion ,当它在 DOM 加载时关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56112816/

相关文章:

php - WordPress Timber 获取自定义帖子类型的帖子

javascript - 如何使所有 Accordion 在浏览器中打开并默认在移动 View 中关闭?

twitter-bootstrap - Bootstrap Accordion - 使用 <span> 而不是 <div> 来显示更多正文

javascript - 页面重新加载后从本地存储中检索复选框集合的保存状态

javascript - 具有 FLOAT 纹理的帧缓冲区上的 webGL 2 readPixels

javascript - ShareThis WP 插件 - 禁用 onhover?

accordion - 如何使 Material UI 扩展面板在扩展时向上打开而不是向下扩展?

javascript - 如何在 Javascript 中将数组更改为对象?

c# - 使用 Watin 选择一个选项

css - 在 wordpress 的 php 文件中查找标签