如何在jquery中替换Anchore标签href中的一些文本
How to replace some of text in href of Anchore tag in jquery
我很少有带有 URL 的锚点,例如"
http%3a//test.dev.test.com/posts/test test",我想在窗口加载函数上将其替换为"http://test.dev.test.com/posts/test 测试",我已经尝试了以下代码,但它没有更新锚标签的 URL。
$('a[href*="http%3a//"]').each(function () {
this.href = this.attr('href').replace('http%3a//', 'http://');
});
我不明白为什么它不起作用。 请帮助我。
编辑::
<a href="http%3a//test.dev.test.com/posts/test test" rel="bookmark" class="aChangeUrl" id="ctl00_lnkEntry">New test in March</a>
>attr
不是原生的DOM方法,而是jQuery方法。
改变
this.href = this.attr('href').replace('http%3a//', 'http://');
自
this.href = $(this).attr('href').replace('http%3a//', 'http://');
请注意,您可以使整体更干净,更简单:
$('a[href^="http%3a//"]').attr('href', function(_,s) {
return s.replace('http%3a//', 'http://');
});
尝试使用.attr()
函数的接收器函数来简化您的工作,
$('a[href*="http%3a//"]').attr('href', function(i,href){
return href.replace('http%3a//', 'http://');
});
您面临的错误的原因:
this.attr('href')
----^ Here comes the problem. 'this' will point a native javascript
object at this context and it doesn't have a member function called .attr().
相关文章:
- 名称href标签根据用户文本输入JavaScript
- 如何使用 jQuery 替换文本中的 HREF 链接
- 如何使用Javascript更改href和html文本
- 使用php使用href链接将输入的文本框值传递到另一个页面
- CSS/JS:如何在悬停时显示文本,并显示点击href的文本框
- Javascript/jQuery链接/href补充的动态文本
- 使用javascript将href文本分解为多行
- 将文本框数据传递到JavaScript窗口.location.href
- 如何在jquery中替换Anchore标签href中的一些文本
- 制作不带 href 的过渡文本
- 如何获取所选锚文本的标记名称及其 href
- 将 href 值替换为 asp.net 中的标签文本
- 引导按钮组以动态更改文本、HREF 和图标 - JQuery
- 使用 .innerHTML 设置文本并更改 href
- 使用 Javascript 替换 HTML 中的 href 文本
- 替换 href 属性中文本的值
- 删除 href 周围的文本的正确 jquery 函数是什么
- IE8 中的 jQuery Change attr href 会更改文本
- 使用Javascript将href文本替换为其他内容
- Javascript初学者:如果href文本与指定字符串匹配,如何替换它