jQuery check if ANY element exists

时间:2021-04-21 20:12:14

I have this code which is not working jQuery

我有这个代码不能正常工作jQuery

if($("#dis(h3)").length == 0) $("#dis").append("<p>no display</p>");

HTML:

<div id="dis">
<h3>Title</h3>
//append here
</div>

The append code should work

附加代码应该有效

<div id="dis">
<h3>Title</h3>
<p>Text</p>
</div>

The append code should not work.

附加代码不起作用。

Edit: I need the append code to appear if there is no other element inside #dis beside h3

编辑:如果h3旁边的#dis中没有其他元素,我需要显示附加代码

What should i do to make it as above(intended)

我应该怎么做才能如上所述(打算)

1 个解决方案

#1


1  

Try

if($("#dis > h3").siblings().length == 0) $("#dis > h3").append("<p>no display</p>");

See this fiddle

看到这个小提琴

#1


1  

Try

if($("#dis > h3").siblings().length == 0) $("#dis > h3").append("<p>no display</p>");

See this fiddle

看到这个小提琴