本文介绍了从标签中的href获取查询字符串参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一系列< a> 链接的页面,如下所示:

 < a href =http://www.domain.com/page/other.jsf?fruit=apple&tree=pine&rock=sedimentary>点击我< /一个> 

这些参数以较小的方式处理链接到(other.jsf)的最终页面的内容。作为A / B / n测试的一部分,我希望根据用户在包含链接的页面上的行为来更改href中的参数。 所以,例如,我想在点击之前将水果参数更改为橙色。



我的问题是参数更改字符串中的位置或可能根本就不存在某些链接,并且.param()函数只能用于网址。



到目前为止(基于另一个问题答案)我有这个,但它没有考虑到可能没有end_pos或者href缺少fruit =的可能性(虽然这个第二位看起来像是一个更易于修复的函数):

($(this).attr('href'))

  ).indexOf('other')> -1){
var hrefStr = $(this).attr('href');
var start_pos = hrefStr.indexOf('fruit =')+ 1;
var end_pos = hrefStr.indexOf('&',start_pos); //只要fruit = apple位于字符串的中间或前面,就可以工作
var fruit = hrefStr.substring( start_pos,end_pos);
...
//将修改后的href放回< a>
}
});

然后我的第二个问题是识别原始href的相同部分以将新字符串放回。我可以理解提取它的方法之后,也许可以得到这个。虽然.b
$ b

我也应该说我不能控制.jsf页面而不是通过JavaScript 您可以使用 a [href * = fruit]包含fruit at href 属性的code> a a href =http://api.jquery.com/attr/#attr-attributeName-function =nofollow> .attr(attributeName,function) , String.prototype.replace()用替换apple橙色



$(a [href * = fruit])。attr(href,function (_,href){return href.replace(/ apple /, orange)}) < script src = https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js\"></script><a href =http://www.domain.com/page

>

I have a page with a series of <a> links on it that look like this:

<a href="http://www.domain.com/page/other.jsf?fruit=apple&tree=pine&rock=sedimentary">click me</a>

These parameters manipulate the content for the eventual page linked to ("other.jsf") in minor ways.

As part of an A/B/n test I want to change the parameters in the href based on user behaviour on the page containing the link. So, for example, I'd want to change the fruit parameter to "orange" before it was clicked.

My problem is that the parameters change location in the string or may not exist at all for some links and the .param() function only appears to work on urls.

So far (based on another question answer) I have this, but it doesn't account for the possibility that there may be no "end_pos" or if "fruit=" is missing from the href (though this second bit seems like an easier fix with an if undefined-type function):

$('a').each(function () {
    if ($(this).attr('href').indexOf('other') > -1) {
        var hrefStr = $(this).attr('href');
        var start_pos = hrefStr.indexOf('fruit=') + 1;
        var end_pos = hrefStr.indexOf('&',start_pos); //works as long as fruit=apple is in the middle or front of the string
        var fruit = hrefStr.substring(start_pos,end_pos);
        ...
        //put modified href back in <a>
    }
});

My second problem is then identifying the same part of the original href to put the new string back into. I can probably get this after understanding the method for extracting it in the first place though.

I should also say I have no control over the .jsf pages other than via JavaScript

解决方案

You could use Attribute Contains Selector [name*="value"] "a[href*=fruit]" to select a elements that contain "fruit" at href attribute, .attr(attributeName, function) , String.prototype.replace() to replace "apple" with "orange"

$("a[href*=fruit]").attr("href", function(_, href) {
  return href.replace(/apple/, "orange")
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<a href="http://www.domain.com/page/other.jsf?fruit=apple&tree=pine&rock=sedimentary">click me</a>

这篇关于从标签中的href获取查询字符串参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 16:54