【发布时间】:2015-03-10 21:39:14
【问题描述】:
- 目标是让函数追加或截断特定的锚点 href“文本值”
-
标签对象数组和类名与字符串一起传递以附加或截断到 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