简体   繁体   English

将点击事件添加到 div 的子 div,其中子 div 有一个子 a-tag

[英]adding click events to a div's child div's, where the child div have a child a-tag

I have one <div> , where there's a lot of <div> s inside of it, and every child <div> gotta an <a> (anchor tag).我有一个<div> ,里面有很多<div> ,每个孩子<div>必须有一个<a> (锚标签)。

so what i'm trying to do is attach click events to the a-tags.所以我想要做的是将点击事件附加到 a 标签。 It's been working with a ul listed of mine divs.它一直在使用 ul 列出的我的 div。 but i need it to be divs.但我需要它是 div。

working code below:下面的工作代码:

jQuery : jQuery

$('#filterOptions li a').click(function (e) {});

HTML : HTML :

<ul id="filterOptions">
 <li class="active"><a href="#" class="all">All</a></li>
 <li><a href="#" class="prem">Premier League</a></li></li>
</ul>

An what I've tried which also didn't work is:我尝试过但也不起作用的是:

jQuery : jQuery

$('#filterOptions div a').click(function (e) {});

HTML : HTML :

<div class="filterOptions">
    <div class="col-md-1 col-md-offset-1">
      <a href="#" class="all">All</a>
    </div>
    <div class="col-md-2">
      <a href="#" class="league2 ">Premier League>
    </div>
</div>

Please help to me achieve it.请帮助我实现它。

The issue is you have class filterOptions in html markup and you are using id # in the jQuery code.问题是您在 html 标记中有类filterOptions并且您在 jQuery 代码中使用 id # It should be like:它应该是这样的:

$('.filterOptions div a').click(function (e) {});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM