【问题标题】:Extracting anchor href text value from an array of tags in Javascript从Javascript中的标签数组中提取锚href文本值
【发布时间】:2015-03-10 21:39:14
【问题描述】:
  1. 目标是让函数追加或截断特定的锚点 href“文本值”
  2. 标签对象数组和类名与字符串一起传递以附加或截断到 href“文本值”

            <table>
                <tr>
                    <td class="columnOne">
                        <span class="processTitle"> 
                            <a href="sample_US.html">Sample (US version)</a> 
                        </span> 
                        <span class="updateIndicator">12/28/2014</span> 
                    </td>
                    <td>
                        <div class="loc_us">
                            <span class="templateName">Request Form</span>
                            <span class="templateLinks"> 
                                <a target="_blank" href="Templates_Form.xls" class="linkTemplate"></a> 
                            </span>
                            <span class="processTitle"> 
                                <a href="sample2_US.html">Sample2 (US version)</a> 
                            </span> 
                        </div>
                    </td>
                </tr>
            </table>
    

初始化函数调用追加/截断 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;
            }
        }
    }
}
}

我尝试了各种变体来提取文本字符串值,但均未成功,但考虑到对象是在数组中传递的,GetElementByID.GetByTagName 等更简单的方法似乎不起作用。还尝试仅使用 Javascript。

我可能在这里遗漏了一个基本点,并提前感谢您帮助我持续学习!

【问题讨论】:

    标签: javascript text anchor href


    【解决方案1】:

    在 Javascript 中,对象中定义了两个方法:

    getAttribute(attributeName)setAttribute(attributName, 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);
    }
    

    【讨论】:

    • 有道理!这一切都有效到 setProperty 的点,它将 link.setProperty 标记为不是一个函数。我同时使用 FF 和 IE11 进行测试。
    • 对不起,应该是setAttribute('href', linkHref);
    【解决方案2】:

    要获取 href 的值,请直接在链接上读取 href 属性:

    theLink = theLinkTags_ar[j].href; 
    

    为了更容易选择所需的元素,请考虑:

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

    如果你想要其中的 A 元素:

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

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

    【讨论】:

      【解决方案3】:

      bleepzter 上面使用 .getAttribute('href') 返回 href 文本值的回答效果很好。

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

      最终sn-p:

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

      谢谢!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-04
        • 2019-07-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多