父级单击时,jQuery Click子级 [英] JQuery Click Child when Parent Clicked

查看:91
本文介绍了父级单击时,jQuery Click子级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天很漫长....

我下面有这段代码

<div class="background" onclick="javascript:$('this checkbox').click();">
    <div class="picture"> 
       <img class="colors" src="458x395.png" />
    </div>
    <input type="checkbox" value="nothing" name="check" class="selsts" />
</div>
<div class="background" onclick="javascript:$('this checkbox').click();">
    <div class="picture"> 
       <img class="colors" src="asdfag3.png" />
    </div>
    <input type="checkbox" value="aff4t" name="check" class="selsts" />
</div>

它应该单击该复选框.我无法通过名称,类或ID进行区分,我必须使用类型.我还需要确保它在父div内,并且没有其他内容.

And its supposed to click the checkbox. I can't distinguish by name, class or id, I have to use the type. I also need to make sure its within the parent div and nothing else.

有人知道为什么这行不通吗?我敢肯定我只是一个愚蠢的时刻. :\

Any idea why this isn't working? I'm sure i'm just having a stupid moment. :\

谢谢

推荐答案

HTML

<div class="background">
    <div class="picture"> 
       <img class="colors" src="458x395.png" />
    </div>
    <input type="checkbox" value="nothing" name="check" class="selsts" />
</div>

JavaScript

Javascript

$('div.background').click(function() { 
  var checkBox = $(this).find('input:checkbox'); 
   checkBox.attr('checked', !checkBox.attr('checked'));
});

这篇关于父级单击时,jQuery Click子级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆