如果css属性包含空白()或空白,但至少要保留一个段落,如果有更多的内容,那么我将使用css类或不包含css类来隐藏段落。
如果段落为空白或包含空白(注释),请隐藏段落,最好只有css...if,没有其他选项,而只能使用JavaScript/jquery。
// Ideally I don't want to use javascript/jquery
$("p").html(function(i, html) {
return html.replace(/ /g, '');
}); p:nth-child(n+2):empty,
p:nth-child(n+2):blank,
.MsoNormal p:nth-child(n+2):empty,
.MsoNormal p:nth-child(n+2):blank {
margin: 0 0 0px;
display: none;
}
p::before {
content: ' ';
}
p:empty::before {
content: '';
display: none;
}
p:first-child:empty+p:not(:empty)::before {
content: '';
}
p:first-child:empty+p:empty+p:not(:empty)::before {
content: '';
}
p::after {
content: '';
display: none;
p:empty::after {
display: none;
}
p:first-child:empty+p:not(:empty)::after {
content: '';
}
p:first-child:empty+p:empty+p:not(:empty)::after {
content: '';
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div> some text - 1 </div>
<p> </p>
<p> </p>
<p> </p>
<div> some text - 2 </div>
<p> </p>
<div> some text - 3 </div>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<div> some text - 4 </div>
<p> </p>
<p> </p>
<div> some text - 5 </div>
<p></p>
<p></p>
<div> some text - 6 </div>
<p class="MsoNormal"></p>
<p></p>
<b>So above html, I would like to display:</b>
<div> some text - 1 </div>
<p> </p>
<div> some text - 2 </div>
<p> </p>
<div> some text - 3 </div>
<p> </p>
<div> some text - 4 </div>
<p> </p>
<div> some text - 5 </div>因此,我试图通过伪类&伪元素,但没有运气。(注意-我有jQuery,它在这里工作,但不想使用它。)
发布于 2018-02-02 10:33:47
据我所知,你不能只用CSS来完成这个任务。
使用jQuery是最简单、最干净的方法。我不明白为什么您有jQuery,但是您不想使用它,但是用纯js来做这件事对我来说更“丑陋”。虽然我给了你两段代码。
JS代码:
// get the elements and transform from HTMLCollection object to array
var array_p = document.getElementsByTagName("P");
array_p = Array.prototype.slice.call(array_p);
array_p.forEach(function(value, index) {
var text = value.innerHTML;
text = text.replace(new RegExp(' ', 'g'), '');
text = text.replace(new RegExp(' ', 'g'), '');
value.style.display = "none";
});例如,我添加了一个jQuery代码,如果您想使用它:
$.each($("p"), function(index, value) {
var text = $(this).html();
text = text.replace(new RegExp(' ', 'g'), '');
text = text.replace(new RegExp(' ', 'g'), '');
if (text.length == 0) {
$(this).css("display", "none");
}})
https://stackoverflow.com/questions/48580358
复制相似问题