最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Badly Formed HTML: How to NOT show a bullet on an empty li element - Stack Overflow

programmeradmin11浏览0评论

I have some html that is created/output from a flash application & I need to show/render it in firefox.

The HTML is badly formed, so it will spit out empty li elements & lists with no ul or ol elements but it will spit out the li elements.

Is there a css flag or any method to not show a li bullet if the li element is empty(see code example of what empty means below)?

If a li element has no innerText, then it shows the bullet. But I dont want it to do that, I want it to NOT show the bullet if there is no innerText in the li element. Is there any way to do this without having to parse HTML?

// example of badly formed HTML output from flash
<textformat leading="2">
    <li>
        <font style=" font-family: 'arial'; font-size: 14px; color: #FFFFFF; letter-spacing: 0px; ">
            <b>Influence and Negotiation</b>
            <font style=" font-size: 12px; color: #000000; ">
            </font>
        </font>
    </li>
</textformat>

// sometimes I get an empty li element, which in Firefox shows the bullet. I want to NOT show the bullet/li element if it is empty
<textformat leading="2">
    <li>
        <font style=" font-family: 'arial'; font-size: 14px; color: #FFFFFF; letter-spacing: 0px; ">
            <!-- empty li element -->
        </font>
    </li>
</textformat>

I have some html that is created/output from a flash application & I need to show/render it in firefox.

The HTML is badly formed, so it will spit out empty li elements & lists with no ul or ol elements but it will spit out the li elements.

Is there a css flag or any method to not show a li bullet if the li element is empty(see code example of what empty means below)?

If a li element has no innerText, then it shows the bullet. But I dont want it to do that, I want it to NOT show the bullet if there is no innerText in the li element. Is there any way to do this without having to parse HTML?

// example of badly formed HTML output from flash
<textformat leading="2">
    <li>
        <font style=" font-family: 'arial'; font-size: 14px; color: #FFFFFF; letter-spacing: 0px; ">
            <b>Influence and Negotiation</b>
            <font style=" font-size: 12px; color: #000000; ">
            </font>
        </font>
    </li>
</textformat>

// sometimes I get an empty li element, which in Firefox shows the bullet. I want to NOT show the bullet/li element if it is empty
<textformat leading="2">
    <li>
        <font style=" font-family: 'arial'; font-size: 14px; color: #FFFFFF; letter-spacing: 0px; ">
            <!-- empty li element -->
        </font>
    </li>
</textformat>
Share Improve this question asked Nov 20, 2011 at 23:39 sazrsazr 25.9k70 gold badges214 silver badges387 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

No, there isn't a way. There's :empty but that doesn't work when there's a <font> element beneath it, and there's no :has in CSS. You can use :has with jQuery, though, and since you've tagged it javascript I assume it's an option:

$('li:has(font:empty)').remove();

Would that work?

If you want to remove the empty li elements (where "empty" means none or only whitespace content), you could do:

function getText(el) {
  if (typeof el.textContent == 'string') {
    return el.textContent;
  }
  if (typeof el.innerText == 'string') {
    return el.innerText;
  }
}

var li, lis = document.getElementsByTagName('li');
var re = /^\s*$/;

for (var i=0, iLen=lis.length; i<iLen; i++) {
  li = lis[i];

  if (re.test(getText(li))) {
    li.parentNode.removeChild(li);
  }
}

Alternatively, you could set the empty LIs to:

 list-style-type: none;

by adding a suitable class, but results might not be consistent across browsers. Some will leave a space for the empty li, some will remove it. You could also use a class with:

 display: none;

to leave the li there but not display it and not have a space. It's up to you.

$(document).ready(function(){
    $('li').each(function(){
      if($(this).html().length == 0)
          $(this).css('list-style-type','none')                        
   });

});

http://jsfiddle/yU8aJ/

发布评论

评论列表(0)

  1. 暂无评论