javascript - 如果 href 包含 X,则更改 href

标签 javascript jquery html

我遇到了一个问题,一开始这似乎很简单。有几个具有“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()))})

ifs 似乎工作正常,但我不知道为什么这部分:

$(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 playground

关于javascript - 如果 href 包含 X,则更改 href,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29556341/

相关文章:

javascript - 如何从表头(th)中获取相应的表列(td)?

php - MouseOver 时启动 PHP-Session

javascript - AJAX 与 Ruby on Rails 结合使用?

javascript - 如何单击未完全覆盖背景并在悬停时消失的对象

javascript - 如何检查字符串是类还是 ID,然后将它们剥离以获得名称?

php - Laravel Blade 模板 Form::open() 到 Html

javascript - 如何获取 HTML DOM 类的原始构造函数?

javascript - 模型没有属性_commissed

jquery - div 中的淡入和淡出文本

javascript - div值改变时执行javascript函数