博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
利用jquery修改href的部分字符
阅读量:7220 次
发布时间:2019-06-29

本文共 3284 字,大约阅读时间需要 10 分钟。

试了好久

 

就一句代码即可。

1 $(document).ready(function(){2     $('a').each(function(){3         this.href = this.href.replace('ys_yinqin', 'others');4     });5 });

 

 

如果是替换href整个字符有以下方法:

 

来源于:http://stackoverflow.com/questions/179713/how-to-change-the-href-for-a-hyperlink-using-jquery/28016553#28016553

Even though the OP explicitly asked for a jQuery answer, you don't need to use jQuery for everything these days.

 

1.纯 javascript写的

A few methods without jQuery:

  • If you want to change the href value of all <a> elements, select them all and then iterate through the : 

    var anchors = document.querySelectorAll('a'); Array.prototype.forEach.call(anchors, function (element, index) { element.href = "http://stackoverflow.com"; });
  • If you want to change the href value of all <a> elements that actually have an hrefattribute, select them by adding the [href] attribute selector (a[href]): 

    var anchors = document.querySelectorAll('a[href]'); Array.prototype.forEach.call(anchors, function (element, index) { element.href = "http://stackoverflow.com"; });
  • If you want to change the href value of <a> elements that contain a specific value, for instance google.com, use the attribute selector a[href*="google.com"]

    var anchors = document.querySelectorAll('a[href*="google.com"]'); Array.prototype.forEach.call(anchors, function (element, index) { element.href = "http://stackoverflow.com"; });

    Likewise, you can also use the other . For instance:

    • a[href$=".png"] could be used to select <a> elements whose href value ends with .png.

    • a[href^="https://"] could be used to select <a> elements with href values that are prefixed with https://.

  • If you want to change the href value of <a> elements that satisfy multiple conditions: 

    var anchors = document.querySelectorAll('a[href^="https://"], a[href$=".png"]'); Array.prototype.forEach.call(anchors, function (element, index) { element.href = "http://stackoverflow.com"; });

..no need for regex, in most cases.

 

2.jquery写的

$("a").attr("href", "http://www.google.com/")

...Will modify the href of all hyperlinks to point to Google. You probably want a somewhat more refined selector though. For instance, if you have a mix of link source (hyperlink) and link target (a.k.a. "anchor") anchor tags:

 

...Then you probably don't want to accidentally add href attributes to them. For safety then, we can specify that our selector will only match <a> tags with an existing href attribute:

$("a[href]") //...

Of course, you'll probably have something more interesting in mind. If you want to match an anchor with a specific existing href, you might use something like this:

$("a[href='http://www.google.com/']").attr('href', 'http://www.live.com/')

This will find links where the href exactly matches the string http://www.google.com/. A more involved task might be matching, then updating only part of the href:

$("a[href^='http://stackoverflow.com']") .each(function() { this.href = this.href.replace(/^http:\/\/beta\.stackoverflow\.com/, "http://stackoverflow.com"); });

The first part selects only links where the href starts with http://stackoverflow.com. Then, a function is defined that uses a simple regular expression to replace this part of the URL with a new one. Note the flexibility this gives you - any sort of modification to the link could be done here.

转载地址:http://joqym.baihongyu.com/

你可能感兴趣的文章
【Unity】第7章 输入控制
查看>>
二叉树的性质
查看>>
git cherry-pick的使用
查看>>
python redis插件安装
查看>>
docker 开启远程
查看>>
CRM协同8.2升级到9.2SP2步骤
查看>>
hdu1085
查看>>
C#之获取年、月、日、时、分、秒...
查看>>
函数的不定长参数
查看>>
转载:一碗牛肉面的思考
查看>>
Story Of Web Background
查看>>
emotion使用笔记
查看>>
Discuz3.4-SSRF-从触发点到构造payload
查看>>
01 jmeter性能测试系列_Jmeter的体系结构
查看>>
linux -- Ubuntu 安装搜狗输入法
查看>>
ns-2 tcp-udp模拟实验
查看>>
『转』古老的IRC命令
查看>>
使用只有头结点的链表实现栈
查看>>
equals方法
查看>>
PreparedStatement/Statement处理insert update等操作时乱码,以及URL
查看>>