[英]How to access newly created elements by pure js?
我做了一些“警告”層來代替“ alert()”。
code :
warning = document.getElementById('warning'); // This line is what exactly I want to make functional. the code out side of warning(); This line runs before 'elem:div#warning' is made by warning().
function warning() {
a = document.createElement('div');
document.body.appendChild(a);
a.id = 'warning';
a.className= 'warning';
}
但是我不知道如何通過javascript訪問該新創建和添加的元素。
我認為CSS與新添加的元素可以正常工作。 但是js不是。
我搜索了谷歌,發現一些方法使用原型自動注冊了新創建的元素,但是這對我來說很難理解...
因此,我想做的是,通過創建該“ a”之前執行的javascript訪問新創建的元素“ a”。
我嘗試了這種方法:
if(document.getElementById('warning')) {alert('asdf')};
但這沒用...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.