首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果href包含X,则更改href

如果href包含X,则更改href
EN

Stack Overflow用户
提问于 2015-04-10 08:05:50
回答 1查看 620关注 0票数 0

我有个问题,在乞讨这件事上似乎很无聊。有几个类为“特殊”的对象,对于每个对象,都需要相应地修改参数的href。例如,如果一个url包含'#1‘,那么它应该更改为’这里‘。参见这里的示例:小提琴

代码语言:javascript
复制
<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>
代码语言:javascript
复制
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()))})

ifs看起来工作正常,但我不知道为什么:

代码语言:javascript
复制
$(function(){ 
     $(".special").attr("href", "https://www.example.com/site#3?".concat(URI(window.location.href).query()))
}

不是。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-10 08:41:55

您可以更好地组织您的脚本:

代码语言:javascript
复制
$.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();
代码语言:javascript
复制
<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>

小提琴操场

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29556341

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档