[英]JQuery anchor click function
I am trying a way to click a anchor tag using Jquery which is inside multiple div.我正在尝试一种使用 Jquery 单击多个 div 内的锚标记的方法。
Below is my code:下面是我的代码:
<div class="mainDiv">
<div id="secondDiv" class="check">
<div class="iteratorDiv1" id="id1">
<a href="url">link text</a>
</div>
<div class="iteratorDiv2" id="id2">
<a href="url">link text</a>
</div>
<div class="iteratorDiv3" id="id3">
<a href="url">link text</a>
</div>
<div class="iteratorDiv4" id="id4">
<a href="url">link text</a>
</div>
</div>
</div>
Now if i do something like this现在如果我做这样的事情
$(".iteratorDiv1 a").live('click',function(e)
{
alert("hey working");
});
But using this approach i will have to write this function for iteratorDiv2
, iteratorDiv3
and iteratorDiv4
also.但是使用这种方法,我还必须为
iteratorDiv2
、 iteratorDiv3
和iteratorDiv4
编写这个函数。 Is there any way i can identify the anchor click from the mainDiv
something like below.有什么方法可以从
mainDiv
识别锚点点击,如下所示。 This did not work though.然而这并没有奏效。
$(".mainDiv a").live('click',function(e)
{
alert("hey working");
});
I am just trying to prevent repeatative coding.我只是想防止重复编码。 Any guidance.
任何指导。
Please check the following jsFiddle:请检查以下jsFiddle:
http://jsfiddle.net/amoolya93/1xL9od85/3/ http://jsfiddle.net/amoolya93/1xL9od85/3/
Your code works fine until Jquery version 1.8.3 .For later versions, please use the following:您的代码在 Jquery 版本 1.8.3 之前工作正常。对于更高版本,请使用以下内容:
$('.mainDiv a').on('click',function(e)
{ alert("hey working");
});
I just did some test here, and seem to work.我刚刚在这里做了一些测试,似乎有效。 You might tell jQuery where exactly your "a" is, so you can try something like this:
你可以告诉 jQuery 你的“a”到底在哪里,所以你可以尝试这样的事情:
$(".mainDiv div > a").on("click", function () {
alert("Hey it's working");
});
or或
$(".mainDiv a").on("click", function () {
alert("Hey it's working");
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.