如果href包含X,请更改href
If href contains X, change href
我遇到了一个问题,一开始似乎很麻烦。有几个类为"special"的对象,每个对象都需要根据参数修改href。例如,如果url包含"#1",则应将其更改为"#HERE"。请参阅此处的示例:jsfiddle
<a class="o-btn special" href="https://www.example.com/site#1">open</a><br>
<a class="o-btn special" href="https://www.example.com/site#2">open</a><br>
<a class="o-btn special" href="https://www.example.com/site#3">open</a><br>
<a class="o-btn special" href="https://www.example.com/site#4">open</a><br>
<a class="o-btn special" href="https://www.example.com/site#12">open</a><br>
if ( $(".special").attr("href").indexOf('#1')>-1 )
$(function(){$(".special").attr("href","https://www.example.com/site#HERE?".concat(URI(window.location.href).query()))}
else if ( $(".special").attr("href").indexOf('#2')>-1 )
$(function(){$(".special").attr("href","https://www.example.com/site#2?".concat(URI(window.location.href).query()))}
else if ( $(".special").attr("href").indexOf('#3')>-1 )
$(function(){$(".special").attr("href","https://www.example.com/site#3?".concat(URI(window.location.href).query()))}
else if ( $(".special").attr("href").indexOf('#4')>-1 )
$(function(){$(".special").attr("href","https://www.example.com/site#4?".concat(URI(window.location.href).query()))}
else if ( $(".special").attr("href").indexOf('#5')>-1 )
$(function(){$(".special").attr("href","https://www.example.com/site#5?".concat(URI(window.location.href).query()))}
else if ( $(".special").attr("href").indexOf('#6')>-1 )
$(function(){$(".special").attr("href","https://www.example.com/site#6?".concat(URI(window.location.href).query()))}
else $(function()
$(function(){$(".special").attr("href","https://www.example.com/site?".concat(URI(window.location.href).query()))})
如果似乎是正确的工作,但我不知道为什么部分:
$(function(){
$(".special").attr("href", "https://www.example.com/site#3?".concat(URI(window.location.href).query()))
}
不是。
您可以更好地组织脚本:
$.fn.extend({
fixLink: function () {
return this.each(function (i) {
var thisLink = $(this);
var thisHref = thisLink.attr("href");
var splited = thisHref.split('#');
if (splited.length > 1) {
var hash = splited.pop();
switch( hash ) {
case '1':
thisHref = thisHref.replace('#1', '#HERE');
break;
case '12':
thisHref = thisHref.replace('#12', '#SOMEWHERE');
break;
default:
break;
};
thisHref += '?url=' + encodeURIComponent(window.location.href);
thisLink.attr('href', thisHref);
};
})
}
});
$('.special').fixLink();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<a class="o-btn special" href="https://www.example.com/site#1">open</a>
<br>
<a class="o-btn special" href="https://www.example.com/site#2">open</a>
<br>
<a class="o-btn special" href="https://www.example.com/site#3">open</a>
<br>
<a class="o-btn special" href="https://www.example.com/site#4">open</a>
<br>
<a class="o-btn special" href="https://www.example.com/site#12">open</a>
<br>
Fiddle游乐场
相关文章:
- 如果href包含X,请更改href
- 使用JQuery从包含A href值中设置*all*IMG-src值
- 使用 中包含的 span 标记的内容对 href 标记的列表进行排序
- 当锚点的属性“href”包含javascript时,在后台打开链接
- 如何执行一个包含更多 HREF 地址的链接属性
- 根据链接的 HREF 包含的内容更改 CSS 样式
- 将变量的值包含在 href 字符串 javascript 中
- 使用 Ajax 发送的表单包含多参数和 A href
- 阻止某些链接(包含某些字符串的 href)工作
- javaScript 如果 window.location.href 包含 url1 则不运行,否则运行它
- 在苹果手机野生动物园浏览器中,加载包含href的javascript时收到错误消息
- 在 Jquery 中用锚标记替换所有包含子字符串的字符串,href 属性应该是实际字符串
- 将输入字段设置为包含 window.location.href
- 将v-attr与包含冒号的属性一起使用'xmlns:href'不编译
- 当包含作为href的base64数据的HTML链接元素(<a/>)准备就绪时触发的事件
- 如何在通过javascript知道td的href包含特定文本的情况下找到表行id
- 获得锚html标签的HREF包含javascript
- jQuery:如果此HREF包含
- href包含锚#id的BUG-在跳转到锚之前会在页面顶部停留一瞬间
- 如何删除链接或打开子元素,如果链接属性href包含字符串,我需要找到