2014-12-10 54 views
0

我有很多的HREF在我的HTML:使用Javascript - 基于选择(无JQuery的)动态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>; 

用户也可以手动输入与d的URL和价值。我有S的第二PARAM可以与选择元件

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

我这来获得新的站点代码设置:

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

当这种变化,我想设置的参数s到A,B或C,因此上述网址(如果选择点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>; 

我能明显更新HREF时,该网站的变化,但似乎有点效率低下,如果用户不会帮助类型(例如):

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

在这种情况下,我将如何(为一个更好的词来想要的)抢的参数s(假设我塞进它在一个onChange事件变量)的值,并追加到用户输入的内容(或他们点击的网址)。即让

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

成为

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

感谢 马克

+0

您可以将's'的值放入cookie中。 – Barmar 2014-12-10 21:47:59

+0

它并不总是必要的。短期操作使用长期存储没有意义;它也会降低响应速度,特别是当客户端没有最佳的Internet连接时,由于额外的网络流量和较高的网络延迟。 – 2014-12-11 04:12:52

+0

谢谢。这样可以处理URL的内容,但是当用户只输入:example.com/Doc.html?d=22时 - 即当他们不点击链接时,我需要以某种方式追加网站参数。 (正如标题中提到的,我不想使用JQuery) – mark1234 2014-12-11 12:12:38

回答

0

这个是什么?

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

如果你想只是一些<a>元件(例如,在一个名为<div>),那么你可以用document.getElementById("id")或类似的东西,它返回一个HTML元素单取代document。此外,您可以更改if语句以过滤您想要的任何其他<a>元素。

如果你很好奇,下面是比较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); 
    } 
    }); 
} 
0

几年前,我用一种叫jQuery的烧烤由Ben Alman做到这一点:

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

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

+0

这个问题的标题特别说* no jQuery *。此外,这似乎是一个死去的项目,最后一次犯罪是在4年前。 – 2014-12-11 04:03:13

+0

@impinball是的,因为它不需要更多的工作!它已经过时了,但比快速破解更好!我个人不会使用它,但它的工作原理!当我写这个答案时,标题没有说没有jQuery。 – Neo 2014-12-11 17:51:09