如何 select 直接在具有 id 的元素之后的元素

How to select an element directly after an element with id

我有这个html:

<key id="myId" class="ignoreTheClass"></key>
<string>MyText</string>

我的目标是能够以某种方式更改标签元素(例如 - 更改颜色、编辑文本等)而无需向其添加属性,但我想以某种方式这样做它选择 any 元素,即 #myId 之后的第一个元素。这是必需的,因为它将在用于生成 mobileconfig 文件的程序中使用,这意味着需要多次使用。如果答案包括将 <key> 之后的第一个元素放入子元素中,则 mobileconfig 将无效。

这一切都可以用 JS 完成吗?

在JQuery中:

$('#myId').next()

我想你想要那个。

在纯 JS 中是

<key id="myId" class="ignoreTheClass"></key>
<string>MyText</string>

<script>
    console.log(document.getElementById("myId").nextElementSibling);
</script>