jquery或js:将站点范围内的链接替换为另一个AND,并更改为https

jquery or js: replace a link site-wide with another AND change to https

本文关键字:AND 另一个 https 替换 链接 js 站点 范围内 jquery      更新时间:2023-09-26

我想更改每个实例

http[s]://www.mydomain.com/test/index.php?this_page=foo

https://www.mydomain.com/test/index.php?this_page=bar

我认为它必须能够用attr.replace完成,但我无法弄清楚符号。如何使用jQuery或JavaScript执行此操作?

编辑:到目前为止,似乎每个人都缺少第一部分(http或https对https的更改)或第二部分(foo到bar):) 很抱歉造成混淆

你可以做:

$('a[href$="://www.mydomain.com/test/index.php?this_page=foo"]')
    .attr('href', 'https://www.mydomain.com/test/index.php?this_page=bar');

属性结束选择[docs] 是原生的 CSS3 选择器,因此选择这些元素应该非常快。当然,您也可以对http[s]部分使用正则表达式,例如,.filter ,但这可能会更慢(也取决于您拥有的链接数量)。

如果 URL 包含其他参数,这些参数也应该是新 URL 的一部分,则必须单独处理每个链接:

// Using the attribute-contains selector now. This still assumes that 
// `this_page=foo` is the first parameter in the URL
var pattern = /^https?:'/'/www.mydomain.com/test/index.php?this_page=foo(&|$)/;
$('a[href*="://www.mydomain.com/test/index.php?this_page=foo"]')
  .attr('href', function(i, val) {
      return val.replace(pattern, 'https://www.mydomain.com/test/index.php?this_page=bar$1');
  });

如果this_page可以位于 URL 中的任何位置,则可以获取包含this_page=foo和基本 URL 的所有链接。仅当没有其他以 foo 开头的页面值时,这才有效:

var pattern_page = /([?&])this_page=foo(&|$)/;
$('a[href*="://www.mydomain.com/test/index.php?"][href*="this_page=foo"]')
  .attr('href', function(i, val) {
      return val.replace('http:', 'https:')
                .replace(pattern_page, '$1this_page=bar$2');
  });

一般语法如下:

$('a').prop('href', function(index, value) {
    return newValue;
});

其中newValue的计算将取决于您要如何转换 URL,即:

$('a').prop('href', function(index, value) {
    if (value.match(/=foo'??/) {
        value = value.replace(/=foo'??/, '=bar?').replace(/^http:/, 'https:');
    }
    return value;
});       

我使用了.prop()而不是.attr(),因为您似乎想要访问完全限定的 URL。 使用属性而不是属性会给你 - 属性并不总是包含 URL 方案。

假设这包含在链接的 href 属性中,请尝试以下操作:

$("a").each(function() {
    if($(this).attr("href").indexOf("http:") != -1) {
        $(this).attr("href", $(this).attr("href").replace("http", "https"));
    }
});

可能有更有效的方法,但这应该可以解决问题。

问候理查

这将在整个页面中用 https 替换锚点中的所有 http 引用。

​$.each($('a'), function(i,v){
  var oldULR =  $(v).attr('href');
  var newURL = oldULR.replace('http', 'https');
  $(this).attr('href', newURL);
})​;​

小提琴:http://jsfiddle.net/bkNLD/

替换所有实例!!

$("a").each(function() {
    if ($(this).attr("href") == 'http[s]://www.mydomain.com/test/index.php?this_page=foo') {
        $(this).attr('href', 'https://www.mydomain.com/test/index.php?this_page=foo');
    }
});​

在普通的JavaScript中,你可以使用:

var links = document.links;
for (var i = 0, len = links.length; i < len; i++) {
    href = links[i].href;
    if (href.indexOf('https') == -1) {
        links[i].href = href.replace('http', 'https');
        href = links[i].href;
    }
    if (href.split('=')[1] == 'foo'){
        links[i].href = href.replace('foo','bar');
    }
}​

JS小提琴演示。

在上面,我在可能是错误的假设下工作,即两个替换不一定是相互依赖的。如果是,那么它就变得更简单了:

var links = document.links;
for (var i = 0, len = links.length; i < len; i++) {
    href = links[i].href;
    if (href.indexOf('https') == -1 && href.split('=')[1] == 'foo') {
        links[i].href = href.replace('http', 'https');
        links[i].href = href.replace('foo','bar');
    }
}​

JS小提琴演示。

对于其他任何发现这一点并且和我一样是菜鸟的人...... 这是我最终使用的解决方案。

$(document).ready(function(){ 
  $('a[href*="this_page=foo"]').prop('href', function(index, value) {
  return value.replace(/=foo/, '=bar').replace(/^http:/, 'https:');
  });
});

这是Felix Kling和Alnitak的组合,我非常感谢他们俩。