gpt4 book ai didi

javascript - 如何为不同的元素设置相同的url

转载 作者:行者123 更新时间:2023-12-03 06:07:25 25 4
gpt4 key购买 nike

我尝试为点击事件上的不同元素设置相同的 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/";

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

有什么建议吗?

最佳答案

您可以设置一个主页变量并像这样使用它

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

只需传递window.location.href = homePageUrl

你也可以让它成为自己的函数,例如

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

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

关于javascript - 如何为不同的元素设置相同的url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39478396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com