javascript - 如何动态使用jQuery切换方法?
问题描述
在我的项目中,需要大量的 jQuerytoggle
来改变文本和图标。现在我正在使用:
$("#id1").click(function () {
//Code to toggle display and change icon and text
});
$("#id2").click(function () {
//Same Code to toggle display and change icon and text as above except change in id
});
问题是我要切换的代码太多了,代码很长,但我为每个代码更改的只是 id。所以我想知道是否有任何方法可以使这变得简单。
下面是一个示例图片。我在单页中得到了更多。
解决方案
哦,你能用 aclass
代替id
吗?
<ul>
<li class="idx">A</li>
<li class="idx">B</li>
<li class="idx">C</li>
</ul>
$(".idx").click(function(e){
//Code to toggle display and change icon and text
let target = e.target;
//You can do all what you want just base on the `target`;
});
推荐阅读
- c# - 如何在 Selenium C# (ChromeDriver) 中使用带有登录名和密码的代理
- c# - 将 Unity 游戏中的资源转储到文件中
- javascript - Ajax 加载点击的链接并淡出内容?
- python - 使用python将doc转换为docx
- mysql - Javafx 在登录后返回用户特定的值
- ruby-on-rails - 根据使用同一表的多个关联的条件选择记录
- mysql - 在 MySQL 中将一组行聚合为单个结果
- angular - 如何有效地使用 Angular Material 自定义调色板颜色 Angular Material
- c - 为什么我的打印功能会用以下代码中的最后一个条目覆盖以前的条目?
- javascript - 'if condition' 与 'console.log' 中的相等检查之间的区别