【问题标题】:How to set same url for different elements [duplicate]如何为不同的元素设置相同的网址[重复]
【发布时间】:2017-01-21 13:10:27
【问题描述】:

我正在尝试为点击事件中的不同元素设置相同的 URL:

$(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/";

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

有什么建议吗?

【问题讨论】:

  • 在要更改的元素上放一个通用类?

标签: javascript jquery


【解决方案1】:

你可以设置一个主页变量,然后像这样使用它

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

然后通过window.location.href = homePageUrl

你也可以把它变成自己的功能,比如

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

然后在每个地方以 url 作为参数调用该函数,这样至少您将所有内容都提供给一个函数而不是复制代码

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-21
    • 2018-05-21
    • 2021-11-17
    • 1970-01-01
    • 2017-04-27
    • 2020-01-24
    • 2010-10-22
    • 1970-01-01
    相关资源
    最近更新 更多