如果 div 为空,则隐藏另一个 div 元素

if div is empty, hide a different div element

我有一个博客 post 页面,每个 post 都有 "read more/read less" 切换按钮。

如果“.wpex_div p”为空,我想隐藏 "a.wpex-link"。问题是需要 select 它的 parent/closest link 因为所有 post 都有相同的 class.

<a onclick="wpex_toggle(548787318, 'Read more', 'Read less'); return false;" class="wpex-link" href="#" target="_blank">Read less</a>
<div class="wpex_div" id="wpex548787318" style="display: block;">
    <div class="sidebar-content"></div>
    <p></p>
</div>
$('.wpex_div:has(p:empty)').prev('.wpex-link').hide();

$('.wpex_div:has(p:empty)').prev('.wpex-link').hide();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<a onclick="wpex_toggle(548787318, 'Read more', 'Read less'); return false;" class="wpex-link" href="#" target="_blank">Read less</a>
<div class="wpex_div" id="wpex548787318" style="display: block;">
    <div class="sidebar-content">The p element is empty. That is why the 'Read less' anchor is not displaying</div>
    <p></p>
</div>