![](/img/trans.png)
[英]How to target a specific element without knowing it's class or id, but do have all DOM details of the element
[英]Javascript - How do I target all the inner class with it's parent's unique_id for each element
我有一个具有唯一 ID 的文档文章,其中有一个只有 class 的部分。
<article id="unique_id">
<header>
<h1>Article Header</h1>
</header>
<p>Article with unique id unique_id</p>
<section class="section" contenteditable="true">
<p>Section with uniform class .section</p>
</section>
</article>
如何针对每篇文章的所有 class 部分及其父项的 unique_id(例如“.section#unique_id”)并将其放入 javascript 函数可以处理的变量中。
我只想定位 section 元素,但会有一个基于它的父 id 的唯一引用,所以我可以在更改每个部分的内容时将它们保存到本地存储中。
我也不知道 unique_id,我只针对 class 部分,它的父代的 unique_id 作为参考,不知道父代的特定 unique_id。
我试过document.getElementsByClassName('footer').parentElement();
但会导致错误。
如果您可以提供仅具有默认值且不依赖 jQuery 的中立解决方案,那就更好了。
document.getElementsByClassName('footer')[0].parentNode;
它是一个属性,而不是一个方法:)
如果您的浏览器支持,您可以使用document.querySelectorAll()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.