2015-01-09 156 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