繁体   English   中英

如果href包含X,请更改href

[英]If href contains X, change href

我有一个问题,在开始时似乎很琐碎。 有几个带有“特殊”类的对象,对于每个对象,都需要根据该参数修改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> 

小提琴游乐场

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM