網(wǎng)站首頁 編程語言 正文
前言:
近日丟醬在學(xué)jQuery,在學(xué)習(xí)的時候遇到了一個問題,雖然最后解決了,但是丟醬覺得要把這個問題解決的方法記錄下來,這樣有小伙伴遇到同樣的問題就不會跟我一樣解決好久啦。
我先貼一段正常邏輯的代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="jquery.min.js"></script>
<style>
* {
margin: 0;
padding: 0;
}
a {
text-decoration: none;
}
button {
position: relative;
left: 712px;
top: 97px;
}
.content {
width: 500px;
height: 30px;
line-height: 30px;
border: 1px solid #bbb;
margin: 100px auto;
border-collapse: collapse;
}
th {
background-color: rgb(27, 126, 184);
color: #fff;
border: 1px solid #bbb;
}
td {
border: 1px solid #bbb;
}
</style>
</head>
<body>
<button>添加數(shù)據(jù)</button>
<table class="content" align="center">
<tr>
<th style="font-weight: 700;">課程名稱</th>
<th style="font-weight: 700;">所屬學(xué)院</th>
<th style="font-weight: 700;">已學(xué)會</th>
</tr>
</table>
<script>
$(function() {
var t1 = $('<tr align="center"><td>javaScript</td><td>傳智播客-前端與移動開發(fā)學(xué)院</td><td><a href="javascript:;" class="del">GET</a></td></tr>');
var t2 = $('<tr align="center"><td>css</td><td>傳智播客-前端與移動開發(fā)學(xué)院</td><td><a href="javascript:;" >GET</a></td></tr>');
var t3 = $('<tr align="center"><td>html</td><td>傳智播客-前端與移動開發(fā)學(xué)院</td><td><a href="javascript:;" >GET</a></td></tr>');
var t4 = $('<tr align="center"><td>jQuery</td><td>傳智播客-前端與移動開發(fā)學(xué)院</td><td><a href="javascript:;" >GET</a></td></tr>');
$('button').on('click', function() {
// console.log($('table tr'));
if ($('table tr').length === 1) {
$('table').append(t1);
console.log($(".del"));
// console.log($('table tr').length);
} else if ($('table tr').length === 2) {
$('table').append(t2);
} else if ($('table tr').length === 3) {
$('table').append(t3);
} else if ($('table tr').length === 4) {
$('table').append(t4);
} else {
return false;
}
});
// $("table a").on('click', function() {
// $('table').children('tr').remove();
// })
$('tr').on('click', 'a', function() {
console.log(11);
$(this).parents('tr').remove();
})
})
</script>
</body>
</html>
前端頁面操作流程:
- 1.先點擊“添加數(shù)據(jù)”按鈕,把a標(biāo)簽動態(tài)生成出來。
- 2.再點擊a標(biāo)簽(GET),查看控制臺。
結(jié)果:控制臺啥也沒有輸出
因為這樣寫是有問題的。所有需要修改一下。我先把我修改后的代碼貼出來。后面解釋
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="jquery.min.js"></script>
<style>
* {
margin: 0;
padding: 0;
}
a {
text-decoration: none;
}
button {
position: relative;
left: 712px;
top: 97px;
}
.content {
width: 500px;
height: 30px;
line-height: 30px;
border: 1px solid #bbb;
margin: 100px auto;
border-collapse: collapse;
}
th {
background-color: rgb(27, 126, 184);
color: #fff;
border: 1px solid #bbb;
}
td {
border: 1px solid #bbb;
}
</style>
</head>
<body>
<button>添加數(shù)據(jù)</button>
<table class="content" align="center">
<tr>
<th style="font-weight: 700;">課程名稱</th>
<th style="font-weight: 700;">所屬學(xué)院</th>
<th style="font-weight: 700;">已學(xué)會</th>
</tr>
</table>
<script>
$(function() {
var t1 = $('<tr align="center"><td>javaScript</td><td>傳智播客-前端與移動開發(fā)學(xué)院</td><td><a href="javascript:;" class="del">GET</a></td></tr>');
var t2 = $('<tr align="center"><td>css</td><td>傳智播客-前端與移動開發(fā)學(xué)院</td><td><a href="javascript:;" >GET</a></td></tr>');
var t3 = $('<tr align="center"><td>html</td><td>傳智播客-前端與移動開發(fā)學(xué)院</td><td><a href="javascript:;" >GET</a></td></tr>');
var t4 = $('<tr align="center"><td>jQuery</td><td>傳智播客-前端與移動開發(fā)學(xué)院</td><td><a href="javascript:;" >GET</a></td></tr>');
$('button').on('click', function() {
// console.log($('table tr'));
if ($('table tr').length === 1) {
$('table').append(t1);
console.log($(".del"));
// console.log($('table tr').length);
} else if ($('table tr').length === 2) {
$('table').append(t2);
} else if ($('table tr').length === 3) {
$('table').append(t3);
} else if ($('table tr').length === 4) {
$('table').append(t4);
} else {
return false;
}
});
// $("table a").on('click', function() {
// $('table').children('tr').remove();
// })
$('.content').on('click', 'a', function() {
console.log(11);
$(this).parents('tr').remove();
})
})
</script>
</body>
</html>
前端頁面操作流程:
- 1.先點擊“添加數(shù)據(jù)”按鈕,把a標(biāo)簽動態(tài)生成出來。
- 2.再點擊a標(biāo)簽(GET),查看控制臺。
**結(jié)果:控制臺開始輸出了 **
開始作解釋了。其實大家可以看出來,這里只有一個區(qū)別。我把這兩個貼出來
// 控制臺不輸出
// 通過on事件委派來給tr里面的a標(biāo)簽添加點擊事件
$('tr').on('click', 'a', function() {
console.log(11);
$(this).parents('tr').remove();
});
// 控制臺輸出
$('.content').on('click', 'a', function() {
console.log(11);
$(this).parents('tr').remove();
});
第一個里,我們是事件委派對a標(biāo)簽的元素作單擊事件。但是這種jquery獲取不到動態(tài)添加元素的。
方案解釋:
on雖然可以給未來動態(tài)創(chuàng)建的元素綁定事件,但是要先獲取已擁有的父級元素。然后才能再獲取動態(tài)元素。
就是相當(dāng)于說,給父元素添加一個事件(該事件類型和動態(tài)元素想要實現(xiàn)的事件類型一樣)
所以這里可以得到:
<table class="content" align="center"> <tr> <th style="font-weight: 700;">課程名稱</th> <th style="font-weight: 700;">所屬學(xué)院</th> <th style="font-weight: 700;">已學(xué)會</th> </tr> // 動態(tài)添加的元素 <tr align="center"> <td>javaScript</td> <td>傳智播客-前端與移動開發(fā)學(xué)院</td> <td><a href="javascript:;" class="del">GET</a></td> </tr> </table>
.content是a標(biāo)簽的父級
$('.content').on('click', 'a', function() {
console.log(11);
$(this).parents('tr').remove();
})
只要記住,在動態(tài)生成的元素中,不能直接指定其事件。只能其父標(biāo)簽的事件后,過濾指定特定元素事件。
還有一種寫法:直接指定dom的元素事件。這也是可以的。
$(document).on('click',"a",function(){
console.log("22");
})
原文鏈接:https://blog.csdn.net/DIUDIUjiang/article/details/126284588
相關(guān)推薦
- 2022-12-26 flouting?ui定位組件完美替代ant?deisgn使用詳解_React
- 2022-03-28 Easyx實現(xiàn)窗口自動碰撞的小球_C 語言
- 2022-05-14 .NetCore?Web?Api?利用ActionFilterAttribute統(tǒng)一接口返回值格式及
- 2023-03-17 Python導(dǎo)入Excel表格數(shù)據(jù)并以字典dict格式保存的操作方法_python
- 2022-12-28 jquery點擊獲取動態(tài)數(shù)據(jù)進行傳參問題_jquery
- 2023-01-02 Kotlin?fun函數(shù)使用方法_Android
- 2022-05-13 Django-Cookies && Session
- 2022-12-30 python中的decode()與encode()深入理解_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支