Javascript Jquery单击事件<;a>;标签没有正确发射
我试图捕获带有类的标记上的jquery click事件,但它没有触发 HTML:(有10个街区)Javascript Jquery单击事件<;a>;标签没有正确发射,javascript,jquery,html,events,click,Javascript,Jquery,Html,Events,Click,我试图捕获带有类的标记上的jquery click事件,但它没有触发 HTML:(有10个街区) 由于某些原因,警报总是返回1。我认为您希望将单击回调绑定到链接。您可能还希望阻止默认行为,否则您的页面将重新加载 $(".popup").click(function(){ var id = $(this).parent().parent().attr('id')[5]; alert(id); }); 此外,有效地使用类可以在这些调用失控之前切断大部分parent调用。比如说 &
由于某些原因,警报总是返回1。我认为您希望将单击回调绑定到链接。您可能还希望阻止默认行为,否则您的页面将重新加载
$(".popup").click(function(){
var id = $(this).parent().parent().attr('id')[5];
alert(id);
});
此外,有效地使用类可以在这些调用失控之前切断大部分parent
调用。比如说
<!-- Make the head div belong to class "head" -->
<div id="block1" class="head">
<div class="slideshow">
<a class="popup" href="#imgform">
$(".popup").click(function(){
// If you click the first link, you'll get "block1" in the alert.
var id = $(this).parents(".head").attr('id');
alert(id);
});
$(“.popup”)。单击(函数(){
//如果单击第一个链接,您将在警报中看到“block1”。
var id=$(this.parents(“.head”).attr('id');
警报(id);
});
在事件处理程序中,需要使用实际单击的事件
$('a.popup').click(function() {
var $id = $(this).parent().parent().attr('id');
...
});
请注意,从jQuery 1.7开始,.live()方法已被弃用。使用.on()或.click()附加事件处理程序 试试这个
$('a.popup').on('click',function(){
alert($(this).parent().parent().attr('id')); // to check whether it is firing....
});
使用:
如果您使用的是jQuery 1.7+,请使用:
$(document).on('click', 'a.popup' ,function(){
//do Stuff
});
这可能对你有帮助
data
参数,这可以确保正确转义查询参数。如果你仍然坚持手工操作,你可以使用此外,使用
data-*
属性,您可以轻松删除id
属性解析,并获得更清晰的标记
块
类名数据块id
属性我也遇到过类似的问题,我无法点击我试过的()比live()好 查看此链接了解更多信息查看页面末尾的最后三个示例您可能会得到更好的解决方案。使用
此
$(document).ready(function() {
$('a.popup').on('click',function(){
var id = $(this).parent().parent().attr('id');
id = id.substring(5);
alert(id);
});
});
你可以用
$(document).on("click", "$popup", function() {
var id = $popup.parent().parent().attr('id');
id = id.substring(5);
alert(id);
$.get('index.php?o='+id, function(data) {
$popup.fancybox({
//do stuff
});
});
live()
已弃用。在上使用,为什么要将单击事件绑定到文档而不是链接?@DaniP。是的,当我试图使用.onAlso时,这是一个打字错误,我想你不应该在变量名中使用$。这不是PHP。如果要将$
用作jQuery选择器,则应仅将其用作变量名的开头。live
不推荐使用。应该改为在
上使用。这是第n次不推荐使用live
,您应该在
上使用。弹出窗口
类锚没有id
属性,那么这有什么作用呢?这是返回id它应该是我真的看不出我做错了什么你原来的var$popup=$('a.popup')
是一个由两个元素组成的数组,当您在没有索引的情况下使用它时,它总是使用var id=$popup.parent().parent().attr('id')中的第一个元素代码>。请尝试在此行中更改var id=this.parent().parent().attr('id')代码>尝试此操作时出现错误:无法获取未定义或null的属性“5”reference@R4VANG3R-是的,我做了更改,当您单击第一个图像时,它应该会提醒1
。我想您需要:var$id=$(this).parents(“div”).eq(1).attr(“id”)@AkhilSekharan-您可以通过有效地使用类和属性来避免它。我对你最后的回答作了评论。很抱歉连接速度慢。谢谢,这看起来很性感。问题是在我的css中,我为每个块设置了样式differently@R4VANG3R,您应该使用类名来设置样式,而不是ID,例如
vs
。如果你不想,那么第一个选择就是你想要的(尽管不推荐)。希望这能有所帮助
$(document).on('click', 'a.popup' ,function(){
//do Stuff
});
$(document).ready(function() {
$('a.popup').each(function() {
var $this = $(this);
var id = $this.parent().parent().attr('id').substring(5);
$this.click(function() {
$.get('index.php', {
o: id
}, function(data) {
$popup.fancybox({
//do stuff
});
});
});
});
});
<div class="block" data-block-id="1">
<div class="slideshow">
<a class="popup" href="#imgform">
<img src="<?php echo 'core/controllers/foto.php?o=1'; ?>" title="1" width="271px" height="413px">
</a>
</div>
</div>
$(document).ready(function() {
$('a.popup').each(function() {
var $this = $(this);
var id = $this.parent().parent().data("block-id");
$this.click(function() {
$.get('index.php', {
o: id
}, function(data) {
$popup.fancybox({
//do stuff
});
});
});
});
});
$(document).ready(function() {
$('a.popup').on('click',function(){
var id = $(this).parent().parent().attr('id');
id = id.substring(5);
alert(id);
});
});
$(document).on("click", "$popup", function() {
var id = $popup.parent().parent().attr('id');
id = id.substring(5);
alert(id);
$.get('index.php?o='+id, function(data) {
$popup.fancybox({
//do stuff
});
});