$( document ).ready(function() {
$(".exmore").before("<span class='rmtrail'>...</span>");
$(document).on("click",".exmore", function(e){
console.log("clicked");
console.log($(this).siblings("rmtrail").html());
$(this).siblings("rmtrail").toggle();
});
});
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<a href='#' class='exmore'>open</a>
</body>
</html>
当单击 .exmore
元素时,我尝试切换插入的 .rmtrail
元素,但 $(this).siblings();
方法不起作用。 console.log()
返回未定义。
有没有办法强制 jQuery
DOM 在插入元素后更新?我一直在网上找,找不到。
最佳答案
rmtrail
是一个类,所以尝试 .rmtrail
$( document ).ready(function() {
$(".exmore").before("<span class='rmtrail'>...</span>");
$(document).on("click",".exmore", function(e){
console.log("clicked");
console.log($(this).siblings(".rmtrail").html());
$(this).siblings(".rmtrail").toggle();
});
});
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<a href='#' class='exmore'>open</a>
</body>
</html>
关于javascript - onclick 切换插入的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48212468/