我怎样才能制作一个改变每一个&#; href'每个元素的属性?

时间:2015-10-15 02:09:00

标签: javascript jquery href userscripts

我已经看到了类似的问题,但它获得了每个类的href属性,我想要每个元素的href属性。然后我想将其更改为我指定的一个链接。他的解决方案看起来像这样:

var a,b,c,d, len;

a = document.getElementsByClassName("item title_link");
len = a.length;

    b = a[i].getAttribute('href');
    c = b.replace("#", "");
    d = "http://www.domainname.com/index/" + c + "/get";
    console.log(d); //just to check them out
}

//window.location.href = d;
//or whatever you wanted to do with that...

有什么方法可以替换每个属性而不是只替换指定的类?

3 个答案:

答案 0 :(得分:1)

Pure Javascript方式:

var a = document.querySelectorAll("a");
[].forEach.call(a, function(elem){
  var b = elem.getAttribute('href');
  var newHref = "http://www.domainname.com/index/" +  b.replace(/#/g, '') + "/get";
  elem.setAttribute("href", newHref);
  console.log(newHref);
});

答案 1 :(得分:0)

以jQuery方式

SELECT ID, Place, Description
      SUM(#_of_Items) OVER(PARTITION BY Place) AS #_of_Items
FROM Your_Table

答案 2 :(得分:0)

在添加jQuery标记时,我假设您接受使用jQuery的答案。使用它非常简单,你必须这样做:

$("[href]").attr("href", "your desired href destination");

Here是一个带示例的jsfiddle。

说明[href]选择器选择具有名为href的属性的所有元素,.attr()函数将其值替换为所需的值。

相关问题