jquery或js:将站点范围内的链接替换为另一个AND,并更改为https
jquery or js: replace a link site-wide with another AND change to https
我想更改每个实例
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的组合,我非常感谢他们俩。
- 当包含另一个asp文件时,是否也包含所有引用的样式和脚本页面
- 使用clickToggle并在单击另一个元素时关闭元素
- 使用javascript将动态表从一个html页面打印到另一个html页
- 如何在android中使用phonegap将文件从一个文件夹移动/复制到另一个文件夹
- 如果使用 lodash 将属性存在于另一个对象中,则向对象添加属性
- 另一个ajax调用中的Jquery ajax调用在for循环中没有按预期工作
- 从Rally获取一个特定的标记,以便计算另一个字段中的值
- 无法从jquery Mobile导航栏重定向到另一个页面
- 如何在JQuery函数中定义一个值,然后调用另一个函数并使用该值
- 是否可以将一个函数输入连接到另一个函数调用的文本
- 字符串在将其传递给另一个活动Android JavaScript时读取Null
- 如何在鼠标悬停时在另一个图像上滑动图像.
- 导航到特定事件的另一个变量页面
- 正在传递JSONP标头's数据参数到另一个文件中的AJAX请求
- 如何在react js中将值从一个组件发送到另一个组件
- 按我自己的类克隆另一个元素的内容和顺序
- 在另一个函数中使用变量this
- 如何在react js中从一个页面导航到另一个页面
- 在另一个函数成功结束后调用该函数
- jquery或js:将站点范围内的链接替换为另一个AND,并更改为https