js auto selector dom by providing id All In One
2022/7/8 6:22:47
本文主要是介绍js auto selector dom by providing id All In One,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
js auto selector dom by providing id All In One
there’s a
global variable
named byid
that references the element
<button id="btn">Subscribe</button> <script> // auto selector by id ✅ btn.addEventListener('click', () => { console.log('click btn'); }); let click = new Event("click"); btn.dispatchEvent(click); </script>
<button id="btn">Subscribe</button> <script> // querySelector const btn = document.querySelector(`#btn`); btn.addEventListener('click', () => { console.log('click btn'); }); let click = new Event("click"); btn.dispatchEvent(click); </script>
demo
Chrome
Safari
closest
closest(selectors);
<article> <div id="div-01">Here is div-01 <div id="div-02">Here is div-02 <div id="div-03">Here is div-03</div> </div> </div> </article>
const el = document.getElementById('div-03'); // the closest ancestor with the id of "div-02" console.log(el.closest('#div-02')); // <div id="div-02"> // the closest ancestor which is a div in a div console.log(el.closest('div div')); // <div id="div-03"> // the closest ancestor which is a div and has a parent article console.log(el.closest("article > div")); // <div id="div-01"> // the closest ancestor which is not a div console.log(el.closest(":not(div)")); // <article>
https://developer.mozilla.org/en-US/docs/Web/API/Element/closest
css parent selector ?
:has
pseudo selector
https://matthiasott.com/notes/css-has-a-parent-selector-now
https://developer.mozilla.org/en-US/docs/Web/CSS/:has
refs
https://javascript.info/searching-elements-dom#document-getelementbyid-or-just-id
©xgqfrms 2012-2020
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载
这篇关于js auto selector dom by providing id All In One的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-19vue2 开发移动端h5 使用那个ui框架比较好?-icode9专业技术文章分享
- 2024-11-19ReactJS结合TypeScript、Vite、Redux和TanStack (React Query) 实战教程
- 2024-11-19Vue3资料入门教程:零基础快速上手指南
- 2024-11-19Vue3资料:新手入门教程与实战指南
- 2024-11-19Vue资料:新手入门vue.js教程与实践指南
- 2024-11-19Vue资料:初学者入门指南
- 2024-11-18tcpdf可以等待vue动态页面加载完成后再生成pdf吗?-icode9专业技术文章分享
- 2024-11-16Vue3资料:新手入门必读教程
- 2024-11-16Vue3资料:新手入门全面指南
- 2024-11-16Vue资料:新手入门完全指南