Javascript-基于Select的动态href(无JQuery)

Javascript-基于Select的动态href(无JQuery),javascript,select,Javascript,Select,我的HTML中有许多HREF: <a id="href7" href="./Doc.html?d=7">7-day</a>; <a id="href14" href="./Doc.html?d=14">14-day</a>; <a id="href30" href="./Doc.html?d=30">30-day</a>; 当此更改时,我想将参数s设置为A、B或C,因此如果选择了站点A,则上述URL将为: &

我的HTML中有许多HREF:

  <a id="href7" href="./Doc.html?d=7">7-day</a>;
  <a id="href14" href="./Doc.html?d=14">14-day</a>;
  <a id="href30" href="./Doc.html?d=30">30-day</a>;
当此更改时,我想将参数s设置为A、B或C,因此如果选择了站点A,则上述URL将为:

 <a id="href7" href="./Doc.html?d=7&s=A">7-day</a>;
  <a id="href14" href="./Doc.html?d=14&s=A">14-day</a>;
  <a id="href30" href="./Doc.html?d=30&s=A">30-day</a>;
在这种情况下,如果我在onChange事件中将参数s填充到变量中,并附加到用户输入的内容或他们单击的url中,我将如何获取参数s的值。i、 制造

http://example.com/Doc.html?d=22 
变成

http://example.com/Doc.html?d=22&s=A 
谢谢 马克

这个怎么样

function setSites(sel) {
  // Reuse your function, just in case other logic becomes needed (validation).
  var siteCode = getSite(sel).replace('$', '$$$$'); // escape for `''.replace()`
  var elements = document.getElementsByTagName("a");

  for (var i = 0; i < elements.length; i++) {
    var element = elements[i];
    var href = element.href;

    // Any <a> without an `href=` attribute should be skipped.
    if (href) {
      if (/[&\?]s=(.*?)[&$]/.test(href)) {
        // There already exists a selection here, and we don't want to duplicate
        // it. Also, the regex is made such that `s=` can be anywhere. The value
        // can be of any length, in case more characters are needed.
        element.href = href.replace(/([&\?]s=).*?([&$])/, '$1' + siteCode + '$2');
      } else {
        // Just append the new selection if it doesn't yet exist.
        element.href += '&s=' + siteCode;
      }
    }
  }
}

几年前,我用Ben Alman的jQuery BBQ来做这件事:


如果您使用它,您可以找出所有罕见的场景,因为与专有的快速黑客功能相比,它是非常防弹的。你还没有解释你在做什么,所以我不能提供更多的建议。

你可以将s的值放入cookie中。这并不总是必要的。短期操作无需使用长期存储;它也会降低响应速度,特别是当客户端没有最好的Internet连接时,因为额外的网络流量加上更高的网络延迟。谢谢。这会处理URL,但不会在用户只键入:example.com/Doc.html?d=22时处理,也就是说,当他们不单击链接时,我需要以某种方式附加站点参数。正如标题中提到的,我不想使用jQuery这个问题的标题明确地说没有jQuery。此外,这似乎是一个死项目,其最后一次提交是在4年前。@impinball是的,因为它不需要更多的工作!这是过时的和旧的,但比一个快速的黑客更好!我个人不会使用它,但它是有效的!当我写下这个答案时,标题并没有说没有jQuery。
http://example.com/Doc.html?d=22 
http://example.com/Doc.html?d=22&s=A 
function setSites(sel) {
  // Reuse your function, just in case other logic becomes needed (validation).
  var siteCode = getSite(sel).replace('$', '$$$$'); // escape for `''.replace()`
  var elements = document.getElementsByTagName("a");

  for (var i = 0; i < elements.length; i++) {
    var element = elements[i];
    var href = element.href;

    // Any <a> without an `href=` attribute should be skipped.
    if (href) {
      if (/[&\?]s=(.*?)[&$]/.test(href)) {
        // There already exists a selection here, and we don't want to duplicate
        // it. Also, the regex is made such that `s=` can be anywhere. The value
        // can be of any length, in case more characters are needed.
        element.href = href.replace(/([&\?]s=).*?([&$])/, '$1' + siteCode + '$2');
      } else {
        // Just append the new selection if it doesn't yet exist.
        element.href += '&s=' + siteCode;
      }
    }
  }
}
function setSites(sel) {
  // Reuse your function, just in case other logic becomes needed (validation).
  var siteCode = getSite(sel).replace('$', '$$$$'); // escape for `''.replace()`

  $('a[href]').each(function () {
    var $elem = $(this);
    var href = $elem.attr('href');

    if (/[&\?]s=(.*?)[&$]/.test(href)) {
      // There already exists a selection here, and we don't want to duplicate
      // it. Also, the regex is made such that `s=` can be anywhere. The value
      // can be of any length, in case more characters are needed.
      $elem.attr('href', href.replace(/([&\?]s=).*?([&$])/, '$1' + siteCode + '$2');
    } else {
      // Just append the new selection if it doesn't yet exist.
      $elem.attr('href', href + '&s=' + siteCode);
    }
  });
}