1. 程式人生 > >事件代理on

事件代理on

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>on</title>
</head>
<body>
<input type="text"><input type="button" value="新增">
<ul>

</ul>
<script src="js/jQuery3.3.1.js"> </script>
<script
> $(function () { $("ul").on("click", "li", function () { //事件代理 console.log($(this).text()) //記住$ 取當前值 }); $("input[type=button]").click(function () { let txt = $("input[type=text]").val(); //input取值用val $(`<li>${txt}</li>`).appendTo("ul");
}) //未來追加的元素 是沒有事件 我們通過事件委託 當你出現點選頁面中的DOM沒有任何反應 //1.DOM是否underfine 2.考慮事件代理 }) </script> </body> </html>