简体   繁体   English

隐藏元素,如果为空或内部有空白(Ionic 2)

[英]Hide element if empty or has white space inside (Ionic 2)

I am building an app in Ionic currently, I need to hide some elements have area empty with some white space inside. 我目前在Ionic中构建一个应用程序,我需要隐藏一些元素,区域内部空白,内部有一些空白区域。 I can execute this with jQuery as seen below. 我可以使用jQuery执行此操作,如下所示。

    jQuery('.course p').filter(function() {

      return $.trim($(this).text()) === ''

    }).remove();

I have a custom.js file imported but nothing seems to be happening? 我有一个custom.js文件已导入但似乎没有发生任何事情? Is there a better way to do this? 有一个更好的方法吗? If so, please give me some advice. 如果是的话,请给我一些建议。

Thanks! 谢谢!

I have added three p elements into the snippet - one with a space, one with a non breaking space and one with text. 我在代码段中添加了三个p元素 - 一个带有空格,一个带有不间断的空格,另一个带有文本。 Then the function iterates over each - determines is content after trimming it to get rid of the whitespace and tehn add removes it. 然后函数遍历每个 - 在修剪它之后确定是内容以去除空白并且tehn add删除它。 So what remains is just the single p element with the content - note that I have put a border around the p' elements to show them. 剩下的只是带有内容的单个p元素 - 请注意,我在p'元素周围放置了一个边框来显示它们。

I have just put a button in so that the initial state can be seen as three p elements and then on the click of the button - two are removed. 我刚刚放入一个按钮,以便初始状态可以看作是三个p元素,然后单击按钮 - 两个被删除。

The first p is essentailly a collapsed div with the border - so it shows as a thick line initially, the second p is the nbsp and shows as an empty box - then the last one is the one with text. 第一个p是essentailly带边框的折叠div - 因此它最初显示为粗线,第二个p是nbsp并显示为空框 - 然后最后一个是带文本的那个。 This is the only one that will remain after clicking the button. 这是单击按钮后唯一保留的。

 $(document).ready(function(){ $('#trimButton').click(function(){ $('.course p').each(function(){ if($(this).text().trim() === ''){ $(this).remove();} }) }) }) 
 .course p{border:solid 1px red; margin-bottom:10px} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="course"> <p> </p> <p>&nbsp;</p> <p> test</p> </div> <hr/> <button type="button" id="trimButton">click me to remove the empty p elements</button> 

From my checks, your code should work fine; 从我的检查,你的代码应该工作正常;

 $('#trim').click(function(){ jQuery('.course p').filter(function() { return $.trim($(this).text()) === '' }).remove(); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="course" > <p style="border:solid 1px red; margin-bottom:10px"> </p> <p style="border:solid 1px red; margin-bottom:10px">&nbsp;</p> <p style="border:solid 1px red; margin-bottom:10px">this is </p> <p style="border:solid 1px red; margin-bottom:10px"> this</p> </div> <button type="button" id="trim">Clear Empty Elements</button> 

May be an issue with the way the js file was imported or the maybe the jquery selector cannot find the element because it is incorrectly named? 可能是js文件导入方式的问题,或者jquery选择器可能找不到该元素,因为它被错误地命名了?

Alternatively, since you are using angularjs, you should consider using ng-if 或者,由于您使用的是angularjs,因此您应该考虑使用ng-if

ng-if="*yourDataSource*.name.indexOf(' ') !== -1"

or as of ES2015 或者从ES2015开始

ng-if="*yourDataSource*.includes(' ')"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM