如何用每个替换元素的attr href? //去除网址 [英] How to replace element's attr href with each ? // strip url

查看:121
本文介绍了如何用每个替换元素的attr href? //去除网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试通过每种方法更改href,

im trying to change href with each method,

这是演示,检查a,您会发现没有变化

html:

<a href="#/news">News</a>
<a href="#/news/detail">Detail</a>
<a href="#/sport">Sport</a>
<a href="#/sport/football">Football</a>​​​​​​​​​​​​

jQuery:

$('a').each(function() {
  $(this).attr('href').replace('#/',''); //tried to erase #/ from all hrefs
});​

推荐答案

您发布的代码将以string的形式获取值,然后以replace的形式正确获取值,但会立即丢弃结果.您需要将替换后的值传递给attr.尝试以下

The code you posted will get the value as a string then properly replace the values but it immediately discards the result. You need to pass in the replaced value to attr. Try the following

$('a').each(function() {
  var value = $(this).attr('href');
  $(this).attr('href', value.replace('#/',''));
});​

这篇关于如何用每个替换元素的attr href? //去除网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆