元素变为未定义
Element becomes undefined
我创建了一个简单的库,我正在尝试更改库中元素的样式。很简单的事情。
content[0].removeChild(content[0].firstElementChild);
content[0].style.backgroundColor = 'green';
我在 Chrome 中尝试过,效果非常好。然后我在 Microsoft Edge 和 Internet Explorer 中对其进行了测试,它们都无法识别我要更改的元素。
他们给出以下错误:
Unable to get property 'removeChild' of undefined or null reference
我怎样才能解决这个问题,让它在所有浏览器中都能正常工作?
function veryUsefullLibrary(parentElem) {
var outerWrapper = document.createElement('div'),
innerWrapper = document.createElement('div'),
content = parentElem.children;
function changeFunction(e) {
console.log(content);
content[0].removeChild(content[0].firstElementChild);
content[0].style.backgroundColor = 'green';
}
while (content.length)
innerWrapper.appendChild(content[0]);
content = innerWrapper.children;
var radio = document.createElement('input');
radio.type = 'radio';
radio.name = 'simSlider';
radio.addEventListener('change', changeFunction);
parentElem.appendChild(radio);
outerWrapper.appendChild(innerWrapper);
parentElem.appendChild(outerWrapper);
}
var overall = document.getElementById('overall'),
lib = new veryUsefullLibrary(overall);
#first {
width: 500px;
height: 386px;
}
<div id="overall">
<div class="content" id="first">
<img src="https://verkoren.files.wordpress.com/2013/05/junk-dump-22918.jpg" />
</div>
</div>
这似乎让它起作用了。
function changeFunction(e) {
content = innerWrapper.children;
console.log(content);
content[0].removeChild(content[0].firstElementChild);
content[0].style.backgroundColor = 'green';
}
我创建了一个简单的库,我正在尝试更改库中元素的样式。很简单的事情。
content[0].removeChild(content[0].firstElementChild);
content[0].style.backgroundColor = 'green';
我在 Chrome 中尝试过,效果非常好。然后我在 Microsoft Edge 和 Internet Explorer 中对其进行了测试,它们都无法识别我要更改的元素。
他们给出以下错误:
Unable to get property 'removeChild' of undefined or null reference
我怎样才能解决这个问题,让它在所有浏览器中都能正常工作?
function veryUsefullLibrary(parentElem) {
var outerWrapper = document.createElement('div'),
innerWrapper = document.createElement('div'),
content = parentElem.children;
function changeFunction(e) {
console.log(content);
content[0].removeChild(content[0].firstElementChild);
content[0].style.backgroundColor = 'green';
}
while (content.length)
innerWrapper.appendChild(content[0]);
content = innerWrapper.children;
var radio = document.createElement('input');
radio.type = 'radio';
radio.name = 'simSlider';
radio.addEventListener('change', changeFunction);
parentElem.appendChild(radio);
outerWrapper.appendChild(innerWrapper);
parentElem.appendChild(outerWrapper);
}
var overall = document.getElementById('overall'),
lib = new veryUsefullLibrary(overall);
#first {
width: 500px;
height: 386px;
}
<div id="overall">
<div class="content" id="first">
<img src="https://verkoren.files.wordpress.com/2013/05/junk-dump-22918.jpg" />
</div>
</div>
这似乎让它起作用了。
function changeFunction(e) {
content = innerWrapper.children;
console.log(content);
content[0].removeChild(content[0].firstElementChild);
content[0].style.backgroundColor = 'green';
}