2016-09-13 66 views
0

我想設置相同的網址上的click事件不同的元素:如何設置相同的網址不同的元素

$(document).ready(function() { 

    var realStateBtn = $('#real-estate-center').parent(); 
    var link = $('#real-estate-center > .linkwidth'); 
    var realStateClass = $('.real-estate-center'); 
    var bankOwnedProps = $('#bank-owned-properties > span'); 
    var findYourHomesValue = $('#find-your-homes-value > span'); 

    realStateBtn.css('zIndex', 9999); 

    realStateBtn.click(function() { 
     window.location.href='https://realestatecenter.bankofamerica.com/'; 
    }); 

    link.click(function() { 
     window.location.href="https://realestatecenter.bankofamerica.com/"; 
    }); 

    realStateClass.click(function() { 
     window.location.href="https://realestatecenter.bankofamerica.com/"; 
    });  

    findYourHomesValue.click(function() { 
     window.location.href="http://realestatecenter.bankofamerica.com/tools/marketvalue.aspx"; 
    }); 

    bankOwnedProps.click(function() { 
     window.location.href="http://realestatecenter.bankofamerica.com/"; 
    }); 
}); 

我打電話一些不同的功能此方法:

window.location.href="http://realestatecenter.bankofamerica.com/";

這就是我想要避免的,我只想使用一次。

有什麼建議嗎?

+1

在您想要更改的元素上添加一個通用類? – dave

回答

1

您可以設置一個主頁變量,只需要使用像這樣

var homePageUrl = "http://realestatecenter.bankofamerica.com/" 

,只是通過window.location.href = homePageUrl

,你也可以把它自身的功能就像

function redirect(path) { 
window.location.href = path 
} 

,然後在每個地方以url作爲參數調用該函數,至少您正在將所有內容都採用一個函數與複製代碼

相關問題