简体   繁体   English

获取内部跨度的文本

[英]Getting the text of an inner span

if i have the following links : 如果我有以下链接:

    <a href="#" onclick="return navigateTo(this)" id="menuList:0:menu" class="normalLink">
        <span id="menulist:0:menuLabel">
            <span > Main Page </span>
        </span>
    </a>        

    <a href="#" onclick="return navigateTo(this)" id="menuList:1:menu" class="normalLink">
        <span id="menulist:1:menuLabel">
            <span > Search Orders </span>
        </span>
    </a>

    <a href="#" onclick="return navigateTo(this)" id="menuList:2:menu" class="selectedLink">
        <span id="menulist:2:menuLabel">
            <span > Orders History </span>
        </span>
    </a>

I want to get the element which has the class name selectedLink and then get the value of the inner span of that element where it says in above example Order History . 我想要获取具有类名称selectedLink的元素,然后获取该元素的内部span的值,该值在上面的示例“示例Order History

I can do the first part of finding the element which has selectedLink class, but i cannot retrive the text inside the inner span of that element. 我可以做的第一部分是找到具有selectedLink类的元素,但是无法检索该元素内部范围内的文本。 Below is my code to find the element with class selectedLink : 下面是我的代码,用于查找具有selectedLink类的元素:

    var elem = this.getElementsByTagName('*');
    for (var i = 0; i < elem.length; i++) 
    {
        var classes = elem[i].className;
        if(classes == 'selectedLink' ){
            alert("ok found it the selected link");
        }
    }

Can anyone suggest a way to retrieve Order History ? 谁能建议一种检索Order History Thanks 谢谢

UPDATE: Follow up question 更新:跟进问题

So the point of this excerise is that, i wanted to add a check where if the page is "Order History" yet the navigation menu had selectedLink style applied to another page (happens when user presses BACK) i wanted JavaScript logic to fix the problem so below is my code: 因此,这excerise的一点是,我想添加一个检查,其中如果页面是“订单历史记录”,但导航菜单已经selectedLink应用到另一个页面样式(当用户按下BACK情况),我想JavaScript逻辑来解决问题所以下面是我的代码:

            //get the element that has "selectedLink" style
           // if the text says "Order History" then style is applied ok  
           var elems =  document.getElementsByClassName("selectedLink");
           if(typeof elems != "undefined"){
               for(var i = 0; i < elems.length; i++){
                   alert(elems.length);
                   var text = elems[i].innerText || elems[i].textContent;
                   alert(text);
                   if(text == "Order History"){
                       alert("its ok");
                   }
                   else { //we're here because the element that has "selectedLink" style in not "order History"

                        //get all elements that have "normalLink" style and see which one is "Order History"
                       var allLinks = document.getElementsByClassName("normalLink");
                       for(var j = 0; j < allLinks.length; j++ ){
                           var curText = allLinks[j].innerText || allLinks[j].textContent;

                           if(curText == "Order History"){
                               alert("there it is at counter " + j);
                               alert(allLinks[j].innerText);

                               //swap the class names
                               allLinks[j].className = "selectedLink";
                               elems[i].className = "normalLink";
                               break;
                           }
                       }
                   }
                }
            }

The changing class names bit isn't working! 更改类名称位不起作用! Am i doing it wrong? 我做错了吗?

UPDATE 2: 更新2:

Fixed, It was my silly mistake : 已修正,这是我的愚蠢错误:

changed the ordering of the following : 更改了以下顺序:

    allLinks[j].className = "selectedLink";
    elems[i].className = "normalLink";

to: 至:

    elems[i].className = "normalLink";
    allLinks[j].className = "selectedLink";

Firstly, just use getElementsByClassName() to get the element, that saves you a huge amount of effort: 首先,只需使用getElementsByClassName()来获取元素,这可以节省大量的工作量:

var link = document.getElementsByClassName("selectedLink")[0]; // new browsers only
var link = document.querySelector(".selectedLink"); // more support, but not in IE7 or lower
var link = $(".selectedLink"); // with jQuery

Then, since that text is the only text, you can just use textContent ( innerText in older IE) 然后,由于该文本是唯一的文本,因此您可以只使用textContent (在旧版IE中为innerText

Your end code is one of these, depending on your target browsers and if you have a framework or not: 您的最终代码是其中之一,具体取决于目标浏览器以及是否具有框架:

var text = link.textContent; // new browsers
var text = link.innerText || link.textContent; // older browers
var text = link.text(); // jQuery

This sounds like a perfect job for jquery. 对于jquery来说,这听起来很完美。 include the jquery library on your page, then use the following code: 在页面上添加jquery库,然后使用以下代码:

var text = $('.selectedLink').first('span').first('span').html();

Try this: 尝试这个:

var elem = this.getElementsByTagName('*');
for (var i = 0; i < elem.length; i++) 
{
    var classes = elem[i].className;
    if(classes == 'selectedLink' ){
        alert("ok found it the selected link");
        var a = elem[i].getElementsByTagName('span')[1].innerHTML;
        alert(a);
    }
}   

JSBin 联合会
http://jsbin.com/epoxej/edit#preview http://jsbin.com/epoxej/edit#preview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM