Can I select only when there's no text around it?

前端 未结 4 964
执笔经年
执笔经年 2021-01-21 16:05

I would like to select anchor tags only when they\'re completely by themselves, that way I can make those look like buttons, without causing anchors within sentences to look lik

4条回答
  •  春和景丽
    2021-01-21 16:32

    jQuery Code Example:

    // this will select '

    ' or '

    text

    ' // but not '

    ' $('p').has('a:only-child').each(function() { const p = $(this); // jQuerify let hasalsotext = false; p.contents().each(function(){ if ((this.nodeType === 3) && (this.nodeValue.trim() !== "")) { hasalsotext = true; return false; // break } }); if (!hasalsotext) { $('a', p).addClass('looks-like-a-button'); } });

提交回复
热议问题