jQuery的。如果找不到,请替换链接

时间:2012-12-31 11:57:05

标签: javascript

我是新手,我继承了一些讨厌的jquery代码。我试图找到一种方法,以便如果发现错误,那么它将用另一个字符串替换该字符串。这是原始代码..

    W.load = function (b) {
    var c, d, g = W.prep; S = ! 0, Q = ! 1, O = x[P], b || _(a.extend(J, a.data(O, e))), ba(l), ba(h, J.onLoad), J.h = J.height? Z(J.height, "y") - M - K: J.innerHeight && Z(J.innerHeight, "y"), J.w = J.width? Z(J.width, "x") - N - L: J.innerWidth && Z(J.innerWidth, "x"), J.mw = J.w, J.mh = J.h, J.maxWidth &&(J.mw = Z(J.maxWidth, "x") - N - L, J.mw = J.w && J.w < J.mw? J.w: J.mw), J.maxHeight &&(J.mh = Z(J.maxHeight, "y") - M - K, J.mh = J.h && J.h < J.mh? J.h: J.mh), c = J.href, V = setTimeout(function () {
        B.show()
    },
    100), J.inline?(Y().hide().insertBefore(a(c)[0]).one(l, function () {
        a(this).replaceWith(z.children())
    }), g(a(c))): J.iframe? g(" "): J.html? g(J.html): $(c)?(a(Q = new Image).addClass(f + "Photo").error(function () {
        //J.title = ! 1, g(Y("Error").text("This image could not be loaded"))
        J.title = ! 1, a(this).href.replace('http://www.old.com','http://www.new.com');
    }).load(function () {
        var a; Q.onload = null, J.scalePhotos &&(d = function () {
            Q.height -= Q.height * a, Q.width -= Q.width * a
        },
        J.mw && Q.width > J.mw &&(a =(Q.width - J.mw) / Q.width, d()), J.mh && Q.height > J.mh &&(a =(Q.height - J.mh) / Q.height, d())), J.h &&(Q.style.marginTop = Math.max(J.h - Q.height, 0) / 2 + "px"), x[1] &&(P < x.length - 1 || J.loop) &&(Q.style.cursor = "pointer", Q.onclick = function () {
            W.next()
        }), m &&(Q.style.msInterpolationMode = "bicubic"), setTimeout(function () {
            g(Q)
        },

我已将代码更改为此..

J.title = ! 1, a(this).href.replace('http://www.old.com','http://www.new.com');

但它不起作用。 GGR!任何帮助都会受到极大的关注。 :)

UPDATE *

上面的代码是我正在处理的部分的完整代码..

更新#2

这个答案有效,但我需要做...

a(this).attr('src', function(i, current){ 
 if ( i === 'http://www.old.com'){
 return current.replace('http://www.old.com','http://www.new.com');
 }
 else if ( i === 'http://www.older.com'){
 return current.replace('http://www.older.com','http://www.new.com');
}
 else ();
 })

这不行!!帮助!

2 个答案:

答案 0 :(得分:1)

尝试

this.href.replace('http://old.com/' , 'http://new.com/');

如果要将当前元素的实际属性更改为新属性,则

this.href = this.href.replace('http://old.com/' , 'http://new.com/');

如果您位于引用<a>元素的处理程序内,那么this引用该对象,您不需要从中创建jquery对象,以访问其属性..所以this.href会直接引用该链接的href属性。


免责声明:这是一个obfusated /编码的脚本,你应该更好地找到生成它的原始..这段代码毫无意义,我们的建议真的在盲目..

从第一眼看,似乎this是图片而不是链接,因此如果您想要显示新图片,则需要更改src属性,而不是{{ 1}}在图像上不存在..

href

但你也应该在那里a(this).attr('src', function(i, current){ return current.replace('http://www.old.com','http://www.new.com');}) 确保console.log(a.fn.jquery)是对a

的引用

答案 1 :(得分:0)

你需要更换吗?如果您只是想更改网址,那么您可以这样做。

J.title = ! 1, $(this).attr('href', 'http://new.com/*');

将把当前网址替换为给定的新网址。