javascript - jQuery slideToggle 动态添加 div
问题描述
我有一些使用slideToggle 的代码。
目前我正在为每一位内容编写jquery,即内容一、内容二、内容三以使用该slideToggle。
我的问题是我想开始添加动态内容,所以我想将硬编码的 content-xx 链接更改为内容并展开,但我不知道该怎么做。
我的代码如下;
<div><a class="expand-one" href="#">[ - ]</a></div>
<div class="clearboth"></div>
<div class="content-one">
// Content in here
</div>
<div><a class="expand-two" href="#">[ - ]</a></div>
<div class="clearboth"></div>
<div class="content-two">
// Content in here
</div>
<div><a class="expand-three" href="#">[ - ]</a></div>
<div class="clearboth"></div>
<div class="content-three">
// Content in here
</div>
<script>
$(document).ready(function(){
$('.expand-one').click(function(){
$('.content-one').slideToggle('slow');
return false;
});
$('.expand-two').click(function(){
$('.content-two').slideToggle('slow');
return false;
});
$('.expand-three').click(function(){
$('.content-three').slideToggle('slow');
return false;
});
});
</script>
正如您在此处看到的,我正在为每一位内容编写小 jquery 块,但这在添加动态内容时不起作用。
有什么方法可以将 expand-xxx 更改为仅扩展,将 content-xxx 更改为仅内容,以便我可以添加尽可能多的内容块,并且仍然为每个单独的内容保留 slideToggle?
解决方案
你可以这样做:
$(document).ready(function() {
$('.expand').click(function() {
$(this).parent().nextAll('.content').first().slideToggle('slow');
return false;
});
});
推荐阅读
- c# - 在 ASP.NET C# 中将 Gridview 行转换为列
- javascript - Qt5 - QML - AirLink RV50 - JavaScript:如何在第三方路由器上自动触发组合框和按钮
- python - 在 Pytorch 教程期间发生 ModuleNotFoundError: 'pycocotools._mask'
- python - 使用 if/else 和 for 循环将项目附加到列表理解中的列表
- python - 使用 selenium 读取多个文本元素时出现错误消息
- c++ - 如何在另一个文件夹中使用 JetAPI 恢复 ES 数据库?
- javascript - React - 带钩子的 5 秒倒计时
- ios - 在 CI 上使用 Fastlane 进行代码签名的最佳方式
- xamarin - 如何在 Xamarin Form 中创建范围指示器?
- oracle - 为 oracle 中间件运行智能更新程序命令时,导致“找不到补丁目标”错误的原因是什么?