1

到目前为止我已经完成了

jQuery:

function addURL(element)
{
var baseZipCode = 48180;
$(element).attr('href', function() {
    return this.href + baseZipCode;
});
}

html:

<a onclick="addURL(this)" href="http://www.weather.com/weather/today/" target="_blank">Click this</a>

我的问题是当用户单击链接并且新窗口打开时一切正常,但是当用户再次单击链接而不刷新时,变量被添加到链接中两次。

例如:

第一次: http ://www.weather.com/weather/today/48180

第二次: http ://www.weather.com/weather/today/4818048180

在您刷新页面之前它不会正确运行,我们将不胜感激。提前致谢

4

4 回答 4

1

将您的addURL功能替换为

function addURL(element)
{
    var baseZipCode = '48180';
    if (element.href.slice(-baseZipCode.length) !== baseZipCode){
        element.href += baseZipCode;
    }
}

没有涉及 jQuery..

于 2013-04-09T16:24:25.283 回答
1

你可以试试jQuery.fn.one

Javascript:

  jQuery("a").one("click", function(){ // Set a handler that execute once
        var baseZipCode = 48180;
        this.href += baseZipCode; //same as "this.href = this.href + baseZipCode"
    });

HTML:

<a href="http://www.weather.com/weather/today/" target="_blank">Click this</a>

也许您需要向 <a> 标签添加一些类以将其与其他标签区分开来

于 2013-04-09T16:26:30.533 回答
0

这应该是:

function addURL(element)
{
    var baseZipCode = 48180;
    element.href = (element.href + baseZipCode)
        .replace(baseZipCode + baseZipCode, baseZipCode);
}
于 2013-04-09T16:19:08.100 回答
0
return this.href.indexOf(baseZipCode) != -1 ? this.href : this.href + baseZipCode;
于 2013-04-09T16:19:47.400 回答