[英]How to access newly created elements by pure js?
I made some 'warning' layer to use instead of 'alert()'. 我做了一些“警告”层来代替“ 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';
}
But I don't know how to access that newly created and added element by javascript. 但是我不知道如何通过javascript访问该新创建和添加的元素。
I think css works fine with newly added elements. 我认为CSS与新添加的元素可以正常工作。 But js is not.
但是js不是。
I searched google, and found some methods register newly created element automatically using prototype, but it was hard to understand to me... 我搜索了谷歌,发现一些方法使用原型自动注册了新创建的元素,但是这对我来说很难理解...
So, what I trying to do is, access to newly created element 'a' by javascript which executed before that 'a' was created. 因此,我想做的是,通过创建该“ a”之前执行的javascript访问新创建的元素“ a”。
I tried this method : 我尝试了这种方法:
if(document.getElementById('warning')) {alert('asdf')};
But it was useless... 但这没用...
Try this fiddle , it works for me, did small change. 试试这个小提琴 ,对我有用 ,做了些零钱。
function warning() {
a = document.createElement('div');
document.body.appendChild(a);
a.id = 'warning';
a.className= 'warning';
}
warning();
warningDiv = document.getElementById('warning');
if(document.getElementById('warning')) {
alert('warning div on DOM')
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.