简体   繁体   English

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

[英]Javascript - Dynamic href based on Select (no JQuery)

I have a number of hrefs in my HTML: 我的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>;

A user can also manually type the URL with and value for d. 用户也可以使用d的和值手动键入URL。 I have a second param of s which can be set with a select element 我有s的第二个参数,可以通过选择元素进行设置

<select id="ddlSite" onchange="getSite(this)">
                <option value="A">Site A</option>
                <option value="B">Site B</option>
                <option value="C">Site C</option>
            </select>

I have this to get the new sitecode: 我有这个来获取新的站点代码:

function getSite(sel) {
        var siteCode = sel.value;
}

When this changes, I want to set the parameter s to A, B or C, so the above URL's (if Site A is selected) would be: 当此更改时,我想将参数s设置为A,B或C,因此上述URL(如果选择了站点A)将为:

 <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>;

I could obviously update the href when the site changes, but seems somewhat inefficient and wouldn't help if the user types (for example): 当网站更改时,我显然可以更新href,但似乎效率低下,并且如果用户输入(例如),则无济于事:

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

In this case how would I (for want of a better phrase) grab the value of parameter s (assuming I stuffed it in a variable on an onChange event) and append to what the user entered (or the url they clicked). 在这种情况下,我(想要一个更好的短语)将如何获取参数s的值(假设我在onChange事件上将其塞入了变量中)并追加到用户输入的内容(或单击的网址)上。 ie make 即使

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

become 成为

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

Thanks Mark 谢谢马克

What about this? 那这个呢?

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;
      }
    }
  }
}

If you want just some of the <a> elements (eg in a named <div> ), then you can replace document with document.getElementById("id") or something similar that returns a single HTMLElement. 如果只需要某些<a>元素(例如,在名为<div>元素中),则可以使用document.getElementById("id")或返回单个HTMLElement的类似内容替换document Also, you could change the if-statement to filter any other <a> elements you would like. 另外,您可以更改if语句以过滤您想要的任何其他<a>元素。

In case you're curious, here's the jQuery solution for comparison: 如果您感到好奇,请使用以下jQuery解决方案进行比较:

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);
    }
  });
}

Years ago I used something called jQuery BBQ by Ben Alman to do this: 几年前,我使用Ben Alman的一种叫做jQuery BBQ的工具来做到这一点:

http://benalman.com/projects/jquery-bbq-plugin/ http://benalman.com/projects/jquery-bbq-plugin/

If you use this you can figure out all the rare scenarios as it is pretty bullet proof, in comparison to a proprietary quick hack function. 如果使用此功能,则与专有的快速hack功能相比,它可以证明所有的罕见情况,因为它们非常安全。 You haven't explained what you're doing so I can't advise much more. 您没有解释您在做什么,所以我不能提供更多建议。

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

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