How to get the Previous Sibling name

后端 未结 2 1345
感动是毒
感动是毒 2021-01-21 06:18

I need to get the name of the previous sibling . to keep it simple i have some sample code



 
   

        
2条回答
  •  悲&欢浪女
    2021-01-21 06:46

    Using jQuery it would be:

    $('#item2').prev().attr("name")​​​​​​​​​​;​
    

    With regular javascript you would need to use the following function (to ensure whitespace nodes are ignored)

    getPreviousSiblingName(document.getElementById("item2"))
    
    function getpreviousSiblingName(element) {
        var p = element;
        do p = p.previousSibling;
        while (p && p.nodeType != 1);
        return p.attributes["name"].value;
    }
    

提交回复
热议问题