Jquery.如果没有找到,请替换链接

Jquery. replace link if not found

本文关键字:替换 链接 如果没有 Jquery      更新时间:2023-09-26

我是一个新手,我继承了一些讨厌的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 !任何帮助都将非常感激。:)

更新*

上面的代码是我正在工作的部分的完整代码…

更新# 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 ();
 })

这行不通!!帮助! !

Try

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属性。


免责声明:这是一个模糊/编码的脚本,你最好找到它产生的原始。这段代码没有任何意义,我们的建议实际上是盲目的。

乍一看,似乎this是一个图像,而不是一个链接,所以如果你想显示一个新图像,那么你需要改变src属性,而不是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是对jQuery的引用

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

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

将当前url替换为给定的新url