admin管理员组文章数量:1022695
I have the following scenario; in my js I have a dynamic hyperlink that I need to capture the clicked link id.
for (var i = 0; i < neighbor.data[3].length; i++){
<a class="Chris" name="a" id="+i+" href="Chris">Hi</a>
}
Now I can get this with;
parseInt($(this).attr("ID"));
I am using this to capture the it;
$(document).on("click", "a.a", function(event) {
event.stopPropagation();
alert(clickedID);
clickedID = parseInt($(this).attr("ID"));
});
Now, my problem is that everytime I click on the hyperlink instead of only one clickedID I have one for every hyperlink clicked. Is there a way to prevent this?
I have the following scenario; in my js I have a dynamic hyperlink that I need to capture the clicked link id.
for (var i = 0; i < neighbor.data[3].length; i++){
<a class="Chris" name="a" id="+i+" href="Chris">Hi</a>
}
Now I can get this with;
parseInt($(this).attr("ID"));
I am using this to capture the it;
$(document).on("click", "a.a", function(event) {
event.stopPropagation();
alert(clickedID);
clickedID = parseInt($(this).attr("ID"));
});
Now, my problem is that everytime I click on the hyperlink instead of only one clickedID I have one for every hyperlink clicked. Is there a way to prevent this?
Share Improve this question edited Sep 22, 2012 at 0:14 Fabrício Matté 70.2k27 gold badges135 silver badges167 bronze badges asked Sep 22, 2012 at 0:13 cbm64cbm64 1,1392 gold badges14 silver badges27 bronze badges 4- 1 Could you elaborate a little more on what's the expected behavior and what's the problem? – Fabrício Matté Commented Sep 22, 2012 at 0:16
-
Why is your click handler using
"a.a"
when your elements have the class"Chris"
? Also, why are you alerting theclickedID
before you set it? Your explanation of what you expect and what actually happens is a bit confusing - perhaps you could create a demo at jsfiddle. – nnnnnn Commented Sep 22, 2012 at 0:17 -
1
What is your for loop supposed to do? Are trying to
document.write
those anchor tags? – Hazem Salama Commented Sep 22, 2012 at 0:23 - 1 $('a.Chris').on('click', function(e){e.stopPropagation();alert($(this).id)}); – Rafael Herscovici Commented Sep 22, 2012 at 0:26
3 Answers
Reset to default 2I believe you are getting every anchor tag because you are setting the event on the document, rather than the anchor tag. Try this instead:
$('a').on('click',function(event) {
event.stopPropagation();
clickedID = parseInt($(this).attr('id'));
alert(clickedID);
});
You have an error in your for loop. Your id="+i+"
will just set all anchor tags' IDs to +i+
because you are not escaping the plus sign and the variable i
You may try this, "a.a"
should be "a.Chris"
because Chris
is the class name you have used not a
andid
should start with a non-numeric character.
for (var i = 0; i < neighbor.data[3].length; i++){
var a='<a class="Chris" name="a" id="id_'+i+'" href="Chris">Hi</a>';
$('#links').append(a);
}
$("#links").on("click", "a.Chris", function(event) { // You can use $(document).on(...) instead of $("#links").on(...)
event.preventDefault();
event.stopPropagation();
clickedID = parseInt($(this).attr("id").split('_')[1]);
alert(clickedID);
});
DEMO.
I have the following scenario; in my js I have a dynamic hyperlink that I need to capture the clicked link id.
for (var i = 0; i < neighbor.data[3].length; i++){
<a class="Chris" name="a" id="+i+" href="Chris">Hi</a>
}
Now I can get this with;
parseInt($(this).attr("ID"));
I am using this to capture the it;
$(document).on("click", "a.a", function(event) {
event.stopPropagation();
alert(clickedID);
clickedID = parseInt($(this).attr("ID"));
});
Now, my problem is that everytime I click on the hyperlink instead of only one clickedID I have one for every hyperlink clicked. Is there a way to prevent this?
I have the following scenario; in my js I have a dynamic hyperlink that I need to capture the clicked link id.
for (var i = 0; i < neighbor.data[3].length; i++){
<a class="Chris" name="a" id="+i+" href="Chris">Hi</a>
}
Now I can get this with;
parseInt($(this).attr("ID"));
I am using this to capture the it;
$(document).on("click", "a.a", function(event) {
event.stopPropagation();
alert(clickedID);
clickedID = parseInt($(this).attr("ID"));
});
Now, my problem is that everytime I click on the hyperlink instead of only one clickedID I have one for every hyperlink clicked. Is there a way to prevent this?
Share Improve this question edited Sep 22, 2012 at 0:14 Fabrício Matté 70.2k27 gold badges135 silver badges167 bronze badges asked Sep 22, 2012 at 0:13 cbm64cbm64 1,1392 gold badges14 silver badges27 bronze badges 4- 1 Could you elaborate a little more on what's the expected behavior and what's the problem? – Fabrício Matté Commented Sep 22, 2012 at 0:16
-
Why is your click handler using
"a.a"
when your elements have the class"Chris"
? Also, why are you alerting theclickedID
before you set it? Your explanation of what you expect and what actually happens is a bit confusing - perhaps you could create a demo at jsfiddle. – nnnnnn Commented Sep 22, 2012 at 0:17 -
1
What is your for loop supposed to do? Are trying to
document.write
those anchor tags? – Hazem Salama Commented Sep 22, 2012 at 0:23 - 1 $('a.Chris').on('click', function(e){e.stopPropagation();alert($(this).id)}); – Rafael Herscovici Commented Sep 22, 2012 at 0:26
3 Answers
Reset to default 2I believe you are getting every anchor tag because you are setting the event on the document, rather than the anchor tag. Try this instead:
$('a').on('click',function(event) {
event.stopPropagation();
clickedID = parseInt($(this).attr('id'));
alert(clickedID);
});
You have an error in your for loop. Your id="+i+"
will just set all anchor tags' IDs to +i+
because you are not escaping the plus sign and the variable i
You may try this, "a.a"
should be "a.Chris"
because Chris
is the class name you have used not a
andid
should start with a non-numeric character.
for (var i = 0; i < neighbor.data[3].length; i++){
var a='<a class="Chris" name="a" id="id_'+i+'" href="Chris">Hi</a>';
$('#links').append(a);
}
$("#links").on("click", "a.Chris", function(event) { // You can use $(document).on(...) instead of $("#links").on(...)
event.preventDefault();
event.stopPropagation();
clickedID = parseInt($(this).attr("id").split('_')[1]);
alert(clickedID);
});
DEMO.
本文标签: jqueryJavascript on event handlerStack Overflow
版权声明:本文标题:jquery - Javascript .on event handler - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745575020a2156965.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论