Hiding an element that contains only spaces using CSS

前端 未结 9 461
不思量自难忘°
不思量自难忘° 2021-01-07 17:34

I am trying to hide the following element in an automatically generated HTML document:

  

9条回答
  •  再見小時候
    2021-01-07 17:49

    Here is my solution which I just implemented for a client using jQuery 1.5.x - you might have to adjust the //skip empty tags but which are valid regular expression string.

    $('*:only-child:empty').each(
        function(index) {
            var currentElement = $(this);
            // skip singleton tags
            if(/^(?:area|br|col|embed|hr|img|input|link|meta|param)$/i.test(currentElement.get(0).tagName) == true) {
                    return
            }
            // skip empty tags but which are valid
            if(/^(?:textarea)$/i.test(currentElement.get(0).tagName) == true) {
                    return
            }
            while (currentElement.parent().children().length == 1) {
                currentElement = currentElement.parent();
            }
            // so 0 or more children for the parent then we hide it
            // we will never have more then 0 children though the :empty takes care of that
            console.log('hidding: ' + currentElement);
            currentElement.hide()
        }
    );
    

提交回复
热议问题