2015-01-09 157 views
0

即时通讯尝试绑定五个不同的元素与click事件具有相同的功能作为事件处理程序,但具有不同的参数。继承人是我的代码:JQuery绑定点击事件问题

$("#areaascensor").bind("click", setearImagen("ascensor")); 
$("#areaflat").bind("click", setearImagen("flat")); 
$("#areaduplex").bind("click", setearImagen("duplex")); 
$("#areasimple").bind("click", setearImagen("simple")); 
$("#areatendales").bind("click", setearImagen("tendales")); 

问题是,当我点击任何元素时,它总是跳转最后一个事件。在这种情况下setearImagen(“tendales”)。不知道为什么:(任何线索?

这里是使用

<div class="morph-button morph-button-large" id="coordenadas"> 
<area shape="rect" id="areaflat" coords="129, 50, 381, 152" href="#" style="border: 1px solid;" data-maphilight="{" strokecolor":"000000","strokewidth":1,"fillcolor":"0000ff","fillopacity":0.4}"=""> 
<area shape="rect" id="areaduplex" coords="129, 154, 383, 283" href="#" style="border: 1px solid;" data-maphilight="{" strokecolor":"000000","strokewidth":1,"fillcolor":"0000ff","fillopacity":0.4}"=""> 
<area shape="rect" id="areasimple" coords="129, 286, 391, 364" href="#" style="border: 1px solid;" data-maphilight="{" strokecolor":"000000","strokewidth":1,"fillcolor":"0000ff","fillopacity":0.4}"=""> 
<area shape="rect" id="areatendales" coords="141, 6, 362, 31" href="#" style="border: 1px solid;" data-maphilight="{" strokecolor":"000000","strokewidth":1,"fillcolor":"0000ff","fillopacity":0.4}"=""> 
<area shape="rect" id="areaascensor" coords="240, 368, 278, 523" href="#" style="border: 1px solid;" data-maphilight="{" strokecolor":"000000","strokewidth":1,"fillcolor":"0000ff","fillopacity":0.4}"=""> 

股利IM我已经用。对和。点击,但仍是同样的问题,试图

这里是setearImagen

function setearImagen(tipo){ 
    if(tipo == "flat"){ 
     $("#interiorImg").html(''); 
     $("#interiorImg").append('<img src="https://drive.google.com/uc?export=view&id=0B9LTjyArujnXUVlCM0FIQUZ1UDg" alt="" width="800" height="500" style="padding-right:10px;"/>'); 
     $("#interiorImg").append('<img src="https://drive.google.com/uc?export=view&id=0B9LTjyArujnXRVY3bEpmVThkVk0" alt="" width="400" height="500"/>'); 
     $("#descripcionDepa").text('Apartamento Flat'); 
    }else if(tipo == "duplex"){ 
     $("#interiorImg").html(''); 
     $("#interiorImg").append('<img src="https://drive.google.com/uc?export=view&id=0B9LTjyArujnXM3ZTdU1MTkRCT3c" alt="" width="800" height="500" style="padding-right:10px;"/>'); 
     $("#interiorImg").append('<img src="https://drive.google.com/uc?export=view&id=0B9LTjyArujnXNjc0ajh3bEM2eTg" alt="" width="400" height="500" style="padding-right:10px;"/>'); 
     $("#descripcionDepa").text('Apartamento Duplex'); 
    }else if(tipo == "simple"){ 
     $("#interiorImg").html(''); 
     $("#interiorImg").append('<img src="https://drive.google.com/uc?export=view&id=0B9LTjyArujnXSmFwaTZ2SGVNSE0" alt="" width="800" height="500" style="padding-right:10px;"/>'); 
     $("#interiorImg").append('<img src="https://drive.google.com/uc?export=view&id=0B9LTjyArujnXcThRcXhveTVlams" alt="" width="400" height="500" style="padding-right:10px;"/>'); 
     $("#descripcionDepa").text('Apartamento Simple'); 
    }else if(tipo == "tendales"){ 
     $("#interiorImg").html(''); 
     $("#interiorImg").append('<img src="undefined" alt="" width="800" height="500" style="padding-right:10px;"/>'); 
     $("#interiorImg").append('<img src="undefined" alt="" width="400" height="500" style="padding-right:10px;"/>'); 
     $("#descripcionDepa").text('Tendales'); 
    }else if(tipo == "ascensor"){ 
     $("#interiorImg").html(''); 
     $("#interiorImg").append('<img src="https://drive.google.com/uc?export=view&id=0B9LTjyArujnXMVRuZHlQdVVzSGM" alt="" width="800" height="500" style="padding-right:10px;"/>'); 
     $("#interiorImg").append('<img src="https://drive.google.com/uc?export=view&id=0B9LTjyArujnXNkUyVW1PMjRhSlE" alt="" width="400" height="500" style="padding-right:10px;"/>'); 
     $("#descripcionDepa").text('Ascensor'); 
    } 
} 
+0

任何的任何其他元素的内部元素?也许你需要告诉我们一些你的HTML – DLeh 2015-01-09 21:17:48

+0

你能发布一个完整的代码示例吗? – j08691 2015-01-09 21:18:59

+0

我没有看到代码的猜测是,也许你的事件是冒泡的dom。根据你使用的jQuery版本,你有两种不同的绑定方法。如果它的jquery 1.7或更低使用绑定,如果它更大,请使用.on – 2015-01-09 21:22:00

回答

0

您正在将click事件绑定到方法的结果在右边调用 - 也就是说,如果函数名后有params,则javascript将立即调用该函数,并且绑定将期望该函数返回可处理单击事件的另一个函数。如果你想通过PARAMS的方法,他们应该在事件名称后,通过一个单独的对象,即:

$('selector').bind('click', {arg: val}, setearImagen); 

可以read more about it here

+0

您好,thx为答案。im tryin $(“#areatendales”)。on(“click”,{value:“tendales”},setearImagen) ;但它会导致错误,是什么意思? – Velroj 2015-01-09 21:43:44

+0

是的,但是你必须改变setearImagen,因为它现在将事件作为参数,而不是你最初想要的值。更多信息 – 2015-01-09 21:55:22

+0

但是现在我已经查看了你的代码,你只需要将你的头发拉出来,请写出单独的处理程序,每个处理程序一个,并停止使用参数和'if'语句 - 它是只是很差的代码 – 2015-01-09 21:58:18