大家好,我想将 div 动态附加到我单击的 div 中。这是代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<script src="query.js">
$('.hello').click(function(){
$(this).append('<div>I am the new one</div>');
});
</script>
</head>
<body>
<div class="hello1">Hello guys</div>
<div class="hello2">Hiiiiiiii</div>
<div class="hello3">Awesome</div>
</body>
</html>
谁能告诉我我的代码有什么问题
最佳答案
您的代码中有 3 个问题
- 脚本标记不能包含 src 和内容
- 由于您的脚本是在元素加载之前放置的,因此您需要将脚本放入 dom 就绪处理程序中
- 您的 html 中没有名为 hello 的类
所以
<div class="hello">Hello guys</div>
<div class="hello">Hiiiiiiii</div>
<div class="hello">Awesome</div>
<script src="query.js"></script>
<script>
jQuery(function ($) {
$('.hello').click(function () {
$(this).append('<div>I am the new one</div>');
});
})
</script>
关于javascript - 无法使用 jquery 的单击事件附加 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32328053/