简体   繁体   中英

How to select an element directly after an element with id

I have this html:

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

I am aiming to be able to change the label element in some way (eg - changing colour, edit text, etc.) without having to add an attribute to it, but I would like to do so in a way it selects any element, that is the first element directly after the #myId . This is required, because this will be used in a program used to generate mobileconfig files, which means this will need to be used more than once. If the answer includes putting the first element after the <key> into a child element, the mobileconfig will be invalid.

Could this all be done in just JS?

In pure JS it's

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

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

In JQuery:

$('#myId').next()

I think you wanted that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM