如何在块中单击按钮后进行不同的操作 块相同的名称类 (Jquery)
How to make different actions after click button in blocks the same name classes (Jquery)?
我有两个具有相同html的块。当我尝试在 js 中单击按钮后添加效果时,它在两个块中都有效。我该如何解决它?谢谢你的帮助。
<div class="class-1">
<div class="red-line"></div>
<div class="first-btn"></div>
<div class="second-btn" style="display:none;"></div>
<div class="my-content">545646</div>
</div>
<div class="class-1">
<div class="red-line"></div>
<div class="first-btn"></div>
<div class="second-btn" style="display:none;"></div>
<div class="my-content">12323</div>
</div>
我的js:
$('.first-btn').click(function() {
$('.red-line').css('display' , 'none');
$('.my-content').css('height' , '500px');
$('.second-btn').css('display' , 'block');
$('.first-btn').css('display' , 'none');
});
首先获取父级,然后选择其中的子项,如下所示。
$('.first-btn').click(function () {
var parent = $(this).parent();
$('.red-line', parent).css('display', 'none');
$('.my-content', parent).css('height', '500px');
$('.second-btn', parent).css('display', 'block');
$('.first-btn', parent).css('display', 'none');
});
您需要在当前元素上下文中识别元素,即 this
.使用.closest()
向上遍历到父元素,然后使用.find()
/.children()
查找所需的元素。
$('.first-btn').click(function() {
var parent = $(this).closest('.class-1');
parent.find('.red-line').css('display' , 'none');
parent.find('.my-content').css('height' , '500px');
parent.find('.second-btn').css('display' , 'block');
$(this).css('display' , 'none');
});
作为替代方案,您也可以使用siblings()
$('.first-btn').click(function() {
$(this).siblings('.red-line').css('display' , 'none');
$(this).siblings('.my-content').css('height' , '500px');
$(this).siblings('.second-btn').css('display' , 'block');
$(this).css('display' , 'none');
});
相关文章:
- 如何使jQuery插件函数可调用以供独立使用,而不在集合上操作
- Jquery菜单操作不稳定,定位不正确,存在一般错误
- Jquery表单验证插件-如果选中复选框,如何在提交时执行某些操作
- Jquery未定义函数正在停止其他操作
- 如何动态操作jquery可拖动UL元素的内容
- 加载更多操作 jquery
- MVC - 在 ajax 调用中操作 Jquery 插件属性
- 使用媒体查询来操作 jQuery 手风琴
- 是否可以将一组类存储在内存中,以便我可以在页面上显示它们之前对它们执行操作?(JQuery)
- 正在操作JQuery.text()输出
- 单击操作 Jquery 不起作用
- 未定义ID/No操作Jquery
- 如何操作jquery-bootgrid格式化程序中的元素
- 操作 jQuery 使用输入创建的输入
- 使用哪种拖放操作?jQuery可拖放还是HTML5原生拖放?或者其他的
- 操作jQuery节点的文本值
- 使用底层客户端JSON数据连接、构建和操作JQuery UI
- 操作jquery集合
- 操作jQuery对象而不改变html元素
- 如果输入字段在数组中找到值,请执行此操作(jQuery/Javascript)