Using $(this).find with .click() jQuery
Using $(this).find with .click() jQuery
单击加号图标时,我希望检查图标仅针对该特定div中的加号图标显示。并非整个页面中的所有加号图标。这是 html 后跟 jQuery。
<div class="pin">
<img class="brand-logo" src="img/nike.jpg" />
<div class= "hover-popup">
<h1 class="brand-name">Nike</h1>
<img class="plus-icon" src="img/plus-icon.png" />
<img class="check-icon" src="img/check-icon.png" />
</div>
</div>
<div class="pin">
<img class="brand-logo" src="img/calvinklein.jpg" />
<div class= "hover-popup">
<h1 class="brand-name">Calvin Klein</h1>
<img class="plus-icon" src="img/plus-icon.png" />
<img class="check-icon" src="img/check-icon.png" />
</div>
jQuery
$(".plus-icon").click(
function () { $(this).find('.check-icon').show(); },
function () { $(this).find('.plus-icon').hide(); }
);
$(".check-icon").click(function() {
function () { $(this).find('.plus-icon').show(); },
function () { $(this).find('.check-icon').hide(); }
);
首先找到包含的div,因为它是.check-icon,.plus-icon
的祖先使用.closest()
,那么你可以使用find()
$(this).closest('.hover-popup').find('.check-icon').show();
你可以
使用jQuery的siblings
选择器。
$(".plus-icon").click(
function () { $(this).siblings('.check-icon').show(); },
function () { $(this).hide(); }
);
$(".check-icon").click(function() {
function () { $(this).siblings('.plus-icon').show(); },
function () { $(this).hide(); }
);
鉴于您的示例 HTML,这将是首选解决方案。
或者,使用 .parent
,然后使用 find
从那里下降。
$(".plus-icon").click(
function () { $(this).parent().find('.check-icon').show(); },
function () { $(this).parent().find('.plus-icon').hide(); }
);
$(".check-icon").click(function() {
function () { $(this).parent().find('.plus-icon').show(); },
function () { $(this).parent().find('.check-icon').hide(); }
);
最后,如果您不确定.hover-popup
元素相对于按钮的位置,请使用.closest
找到它,然后从那里下降。
$(".plus-icon").click(
function () { $(this).closest('.hover-popup').find('.check-icon').show(); },
function () { $(this).closest('.hover-popup').find('.plus-icon').hide(); }
);
$(".check-icon").click(function() {
function () { $(this).closest('.hover-popup').find('.plus-icon').show(); },
function () { $(this).closest('.hover-popup').find('.check-icon').hide(); }
);
使用 Jquery 你的代码将如下所示:
$(document).ready(function(){
$('.plus-icon').click(function(){
$(this).hide();
$(this).siblings('.check-icon').show()
})
$('.check-icon').click(function(){
$(this).hide();
$(this).siblings('.plus-icon').show()
})
})
试试这个
$(".plus-icon").click(
function () { $(this).next('.check-icon').show(); },
function () { $(this).hide(); }
);
$(".check-icon").click(function() {
function () { $(this).prev('.plus-icon').show(); },
function () { $(this).hide(); }
);
可能最简单的方法是使用 .siblings() 函数。
例如
$('.plus-icon').click(function(){
$(this).siblings('.check-icon').show();
});
相关文章:
- jQuery:.click(function(){(element),collapse('show',f
- 具有相同类的jquery-click元素
- angularjs-bindng-click从jquery动态加载DOM
- jquery on click事件来显示内容
- jQuery.on-click事件不会发生
- 追加Jquery后的Binding Click事件不起作用
- 使用Jquery.click显示/隐藏切换,适用于新用户,但不适用于种子
- iframe中的jquery-click元素
- 响应式表设计:使用JQuery使click()展开tr
- Jquery:输入类型=“”;按钮“;使用.click()函数在Internet Explorer和Safari中不起作
- 一旦用jquery更改了类,按钮就没有响应click
- jQuery-click函数不适用于使用innerHTML动态生成的按钮
- .click() 使用 document.getElementById,但不使用 jquery selector
- addEventListener 在 Javascript 中不起作用,但 jQuery Click 正在工作
- jQuery if url hash, click event/activate javascript
- Using $(this).find with .click() jQuery
- double action in one click jquery
- 如何将click jquery函数与此代码一起使用
- localstorage on button click [Jquery]
- Collapse slideToggle on Click jQuery