Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/420.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Javascript中的标记数组中提取锚href文本值 目标是让函数附加或截断特定的锚href“文本值”_Javascript_Text_Anchor_Href - Fatal编程技术网

从Javascript中的标记数组中提取锚href文本值 目标是让函数附加或截断特定的锚href“文本值”

从Javascript中的标记数组中提取锚href文本值 目标是让函数附加或截断特定的锚href“文本值”,javascript,text,anchor,href,Javascript,Text,Anchor,Href,标记对象数组和类名与要追加或截断到href“text value”的字符串一起传递 12/28/2014 申请表 初始化函数调用追加/截断URL的示例: function initProjectType() { theSpanTags_ar = document.getElementsByTagName("SPAN") appendURLs(theSpanTags_ar,"processTitle","?loc=US"); } function appendURLs(whi

标记对象数组和类名与要追加或截断到href“text value”的字符串一起传递


12/28/2014 
申请表
初始化函数调用追加/截断URL的示例:

function initProjectType() {
    theSpanTags_ar = document.getElementsByTagName("SPAN")
    appendURLs(theSpanTags_ar,"processTitle","?loc=US");
}
function appendURLs(whichTag_ar, whatClassName, theAppendString, URLremoveString) {
for(i=0; i<whichTag_ar.length; i++) {                                   // loop thru the tags   
    if (whichTag_ar[i].className === whatClassName){                    // match tag to class
        var theLinkTags_ar = whichTag_ar[i].getElementsByTagName("A")   // extract the Anchor into an array
        for(j=0; j<theLinkTags_ar.length; j++) {                        // loop thru the number of Anchors          
            theLink = theLinkTags_ar[j].nodeValue.href;                 // extract the href "text value" <<<---help?    
            if (URLremoveString) {                                      // if truncate the href value
                if (theLink.indexOf(URLremoveString) != -1) {
                    theLink = theLink.substr(URLremoveString.length)
                    theLinkTags_ar[j].href = theAppendString + theLink;
                }
            } else {                                                    // else append the href value
                theLinkTags_ar[j].href = theLink + theAppendString;
            }
        }
    }
}
}
函数initProjectType(){
SPAN=document.getElementsByTagName(“SPAN”)
附录URL(附录“processTitle”和“loc=US”);
}
函数appendURLs(whichTag_ar,whatClassName,theAppendString,URLRemovesting){

对于Javascript中的(i=0;i),对象中定义了两种方法:

getAttribute(attributeName)
setAttribute(attributeName,value)

因此,对于您的问题,一旦您获得链接的引用,您可能需要使用以下选项:

for(j=0; j<theLinkTags_ar.length; j++)
{
  var link = theLinkTags_ar[j]; //easy access to the link without indexer.

  var linkHref = link.getAttribute('href'); //get the href.

  if (URLremoveString != null )
  {
     if(linkHref.indexOf(URLremoveString) != -1){
        linkHref = linkHref.substr(URLremoveString.length)
        linkHref = theAppendString + linkHref;
     }
  }
  else
  {
     linkHref = linkHref + theAppendString;
  } 

  link.setAttribute('href', linkHref);
}

对于(j=0;j要获取href的值,请直接在链接上读取href属性:

theLink = theLinkTags_ar[j].href; 
要使选择所需元素更容易,请考虑:

var nodes = document.querySelectorAll('span.processTitle");
如果您希望A元素位于其中:

var nodes = document.querySelectorAll('span.processTitle > a');

不确定是否支持IE 8中的复杂选择器,因此您可能希望暂时保留循环方法。

bleepzter在上面的回答中使用.getAttribute('href')返回href文本值,效果非常好

请注意,代替使用link.setProperty('href',linkHref')它将不起作用,但改为link.setAttribute('href',linkHref)解决了这个问题

最后一段:

for(j=0; j<theLinkTags_ar.length; j++)
{
  var link = theLinkTags_ar[j]; //easy access to the link without indexer.

  var linkHref = link.getAttribute('href'); //get the href.

  if (URLremoveString != null )
  {
     if(linkHref.indexOf(URLremoveString) != -1){
        linkHref = linkHref.substr(URLremoveString.length)
        linkHref = theAppendString + linkHref;
     }
  }
  else
  {
     linkHref = linkHref + theAppendString;
  } 

  link.setAttribute('href', linkHref);
}

for(j=0;jIt是有意义的!它一直工作到setProperty,它将link.setProperty标记为非函数。我正在使用FF和IE11进行测试。对不起,这应该是
setAttribute('href',linkHref);
for(j=0; j<theLinkTags_ar.length; j++)
{
  var link = theLinkTags_ar[j]; //easy access to the link without indexer.

  var linkHref = link.getAttribute('href'); //get the href.

  if (URLremoveString != null )
  {
     if(linkHref.indexOf(URLremoveString) != -1){
        linkHref = linkHref.substr(URLremoveString.length)
        linkHref = theAppendString + linkHref;
     }
  }
  else
  {
     linkHref = linkHref + theAppendString;
  } 

  link.setAttribute('href', linkHref);
}