Select element without a child

后端 未结 3 1150
余生分开走
余生分开走 2020-12-20 15:02

I have a page that might one of the following:

33

Or


    <         


        
相关标签:
3条回答
  • 2020-12-20 15:43

    You can't do it with a regular CSS selector, but you can do it in a few lines of JS:

    var element = document.querySelector('#size');
    var b = element.querySelector('b');
    var text = b ? b.innerText : element.childNodes[0].nodeValue;
    
    console.log(text);
    
    0 讨论(0)
  • 2020-12-20 15:43

    The day we'll have CSS Level 4 selectors and the parent selector you'll be able to use a simple selector but for now you can't do it directly.

    You could iterate to find the first text node but here's a hacky solution :

    var text = document.getElementById('size').innerHTML.split(/<.*?>/)[0];
    

    To be used only if you have some idea of the content of your #size element.

    0 讨论(0)
  • 2020-12-20 15:45

    So really you want significant text (ie other than whitespace, because in your second example there's probably tabs and returns between the span start tag and the b) of #size, or, if that doesn't exist, the significant text of its first element:

    // Is text just whitespace?
    function isWhitespace(text){
        return text.replace(/\s+/,'').length === 0;
    }
    
    // Get the immediate text (ie not that of children) of element
    function getImmediateText(element){
        var text = '';
    
        // Text and elements are all DOM nodes. We can grab the lot of immediate descendants and cycle through them.
        for(var i = 0, l = element.childNodes.length, node; i < l, node = element.childNodes[i]; ++i){
        // nodeType 3 is text
            if(node.nodeType === 3){
                text += node.nodeValue;
            }
        }
    
        return text;
    }
    
    function getFirstTextNode(element){
        var text = getImmediateText(element);
    
        // If the text is empty, and there are children, try to get the first child's text (recursively)
        if(isWhitespace(text) && element.children.length){
            return getFirstTextNode(element.children[0])
        }
        // ...But if we've got no children at all, then we'll just return whatever we have.
        else {
            return text;
        }
    }
    
    0 讨论(0)
提交回复
热议问题