<body>
<button id="btn">CLICK</button>
<div id="list"></div>
</ul>
</body>
</html>
<script>
document.getElementById('btn').addEventListener('click',function(){
var span = document.createElement('span');
span.setAttribute('class','aaa');
span.innerHTML = "123";
document.getElementById('list').insertBefore(span, null);
});
document.getElementById('aaa').addEventListener('click',function(){
alert(123);
});
</script>
我要如何才能點擊新加入的時可以alert
<html>
<body>
<button id="add-link">Add link</button>
<div id="link-container"></div>
<script>
document.getElementById('add-link').addEventListener( 'click', () => {
const node = document.createElement('span');
node.innerText = 'link';
node.style.cursor = 'pointer';
node.addEventListener( 'click', () => alert('Link clicked!') );
document.getElementById('link-container').appendChild( node );
});
</script>
</body>
</html>