With below code I'm trying to click a
tag click event from the parent li
tag. But it gives me this error:
Why I want to do this:
When I click the PHP PDO link we need to cursor move in to that text not the li
tag. I'm trying to fix it using this. But I know we can call that href
from the li
click event by getting the a href
attribute and set it into the window.location.href
. But still trying to trigger the a href
click event when I click the li
tag.
HTML
<li class="tags" style="cursor: pointer;">
<a class="link_em" href="?l=1" id="1">List1</a>
</li>
Jquery:
$('li.tags').on('click', function (e) {
$(this).children('a').click();
return false;
});
Error:
I got this error when I use above code.
Uncaught RangeError: Maximum call stack size exceeded
UI:
/
Edited:
I done this ugly thing, but I am still looking proper solution:
$('li.tags a').on('click', function (e) {
e.stopPropagation();
});
$('li.tags').on('click', function (e) {
window.location.href = $(this).children('a.link_em').attr('href');
e.preventDefault();
});
With below code I'm trying to click a
tag click event from the parent li
tag. But it gives me this error:
Why I want to do this:
When I click the PHP PDO link we need to cursor move in to that text not the li
tag. I'm trying to fix it using this. But I know we can call that href
from the li
click event by getting the a href
attribute and set it into the window.location.href
. But still trying to trigger the a href
click event when I click the li
tag.
HTML
<li class="tags" style="cursor: pointer;">
<a class="link_em" href="?l=1" id="1">List1</a>
</li>
Jquery:
$('li.tags').on('click', function (e) {
$(this).children('a').click();
return false;
});
Error:
I got this error when I use above code.
Uncaught RangeError: Maximum call stack size exceeded
UI:
https://jsfiddle/k92dep45/
Edited:
I done this ugly thing, but I am still looking proper solution:
$('li.tags a').on('click', function (e) {
e.stopPropagation();
});
$('li.tags').on('click', function (e) {
window.location.href = $(this).children('a.link_em').attr('href');
e.preventDefault();
});
Share
Improve this question
edited Sep 24, 2017 at 10:41
Elshan
asked Sep 24, 2017 at 9:46
ElshanElshan
7,7055 gold badges76 silver badges108 bronze badges
6
- You might have recursive call of some function on your page. Is this the only jQuery code written on the page ? – Himanshu Upadhyay Commented Sep 24, 2017 at 9:51
-
2
By returning
false
you're stopping thea
element's default behaviour (navigating to a different page) and the event propagates to the parent hence theMaximum call stack....
error – Titus Commented Sep 24, 2017 at 9:51 - @HimanshuUpadhyay no there's more jquery function in above page. – Elshan Commented Sep 24, 2017 at 9:52
- It sort of begs the question why you'd want to do this. – Andy Commented Sep 24, 2017 at 10:09
-
I previously added
a
tag insideli
tag withhref
. But now I want to trigger thathref
attribute when click theli
– Elshan Commented Sep 24, 2017 at 10:12
4 Answers
Reset to default 2You can use below version for the above code
$('li.tags').on('click', 'a', function (e) {
// Do your stuff.
e.preventDefault();
window.location.href= thishref;
});
Here the second argument is the children 'a' in the click event of jQuery.
The problem is that you keep calling the same element which leads to the Maximum call stack size exceeded
.
Look at the below example.
function foo(){
foo();
}
foo();
In the above code, we are calling foo()
again and again and it will also produce the same result.
You need to add event.stopPropagation()
on a
element and prevent event "bubbling" otherwise you create infinite loop and that is why you get that error.
$('li.tags').on('click', function() {
$(this).children('a').click();
});
$('li.tags a').click(function(e) {
e.stopPropagation()
e.preventDefault() // This is just for demo
console.log($(this).text())
})
a {
border: 1px solid black;
}
li {
padding: 50px;
border: 1px solid red;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li class="tags" style="cursor: pointer;">
<a class="link_em" href="?l=1" id="1">List1</a>
</li>
</ul>
You can do it differently by changing the href
of the window
to the href
stored in your a
tag, getted by using this instruction:
$(this).children('a').attr("href");
Try to implement, or run the following code snippet to confirm if its resolving your problem & get the expected render:
$('li.tags').on('click', function (e) {
window.location.href= $(this).children('a').attr("href");
return false;
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li class="tags" style="cursor: pointer;">
<a class="link_em" href="?l=1" id="1">List1</a>
</li>
Edit
After rereading the question, I think that OP has over plicated things (as have I obviously). Added a simpler version of Demo 1 labeled Demo 2. And added Demo 3 which is solution that uses no JavaScript/jQuery just CSS.
Capture Phase
When you need fine grained control over the event chain .addEventListener()
is better suited to handle particular aspects like firing the event on the capture phase instead of the bubbling phase. The reason why is because li.tag
will be before a.link
on the first phase (i.e. capture phase) of the event chain. We assign capture or bubbling phase by assigning the appropriate boolean value to the third parameter:
document.addEventListener('click', function(e) {...}, false);
This is default, which is set at false
for the bubbling phase.
document.addEventListener('click', function(e) {...}, true);
This will make the registered object (e.g.document
in this example) listen and act on the capture phase.
The following demo -- during capture phase -- will:
- assign the clicked
li.tag
as theevent.target
... - ...then invoke
e.stopPropagation()
so thea.link
nested within theli.tag
will not be included in the event chain. The bubbling phase is skipped as well. - Instead, this
a.link
beese.target
on a new event chain because the trigger method.click()
is invoked on it.
Test
The 3rd link of List0 tests whether a.link
is indeed triggered by .click()
or firing during capture or bubbling phase, or is the event.target
. Because each a.link
in List0 has pointer-events:none
which makes a.link
oblivious to any mouse or click event from a user, we can conclude:
that any activation of said
a.link
is entirely dependant upon its parentli.tag
being clicked by a user.during the capture phase, no click event will reach
a.link
due toe.stopPropagation()
andpointer-events:none
it can't bee.target
.bubbling phase isn't even there for
a.link
because ofe.stopPropagation()
As that event chain dies off
.click()
is fired anda.link
goes off like a second stage rocket发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745258823a4619115.html
评论列表(0条)