简体   繁体   English

使用jqLit​​e隐藏和显示html元素

[英]Using jqLite to hide and show html elements

Like any normal person creating a web application using AngularJS, I initially tried using ng-hide/ng-show to make certain elements visible under certain conditions. 像任何使用AngularJS创建Web应用程序的普通人一样,我最初尝试使用ng-hide / ng-show使某些元素在某些条件下可见。 For some reason, this doesn't want to work, and the code is too complex for me to recount it here. 出于某种原因,这是不希望的,并且代码太复杂了,我无法在这里重述。 I figured it would be easy to use jQuery (or at least as much jQuery as Angular has built into it). 我认为使用jQuery很容易(或者至少与Angular内置的jQuery一样多)。 This is what I have so far: 这是我到目前为止的内容:

angular.element(document.querySelector([ELEMENT ID])).off();

The above line works for the purposes of hiding, but I can never get it back. 上一行是出于隐藏目的而工作的,但我永远无法收回它。 In case you're wondering, I'm trying to hide buttons for otherwise unrelated actions. 如果您想知道,我正在尝试隐藏其他不相关操作的按钮。 Using ".on()" for the code above doesn't work. 对上面的代码使用“ .on()”无效。 How does this line need to be written in order for the element to disappear? 为了使元素消失,需要如何编写此行? More importantly, How do I make it reappear? 更重要的是,如何使它重新出现?

ng-show and ng-hide work with boolean values. ng-showng-hide使用布尔值。 Don't use jQuery inside the controllers. 不要在控制器内部使用jQuery。 If you are needy to use that, create directives for that purpose. 如果您需要使用它,请为此创建指令。

Create flag variable in scope of controller. 在控制器范围内创建标志变量。 Set it to true or false 将其设置为truefalse

Now ng-show will show the element if it receives boolean value as true and will hide if it receives false. 现在,如果ng-show收到的布尔值为true,则将显示该元素;如果收到的值为false,则将隐藏。

Vice versa for ng-hide, it will hide if it received true and show if receives false. 反之,对于ng-hide,如果收到true,它将隐藏,如果收到false,则显示。

So decide between either one of them, don't use both. 因此,请在其中任何一个之间做出决定,不要同时使用两者。 So considering flag name is active and it is set to true and you want to show button in beginning. 因此,考虑到标志名处于活动状态并将其设置为true,并且您想在开始时显示按钮。 The code can be: 代码可以是:

angular.module('demo', []).controller('DemoCtrl', function($scope){
  $scope.active = true;
});

And the template will look like: 模板如下所示:

<div ng-app="demo">
  <div ng-controller="DemoCtrl">
    <button type="button" ng-click="active = false" ng-show="active">Hide Me</button>
    <button type="button" ng-click="active = true" ng-hide="active">Reset</button>
  </div>
</div>

We need a small css for this purpose: 为此,我们需要一个小的CSS:

.display-hide {
   display: none;
}

Lets say there is an validation summary we are showing on the page; 可以说我们在页面上显示了一个验证摘要;

<div class="alert alert-danger display-hide">
        <button class="close" data-close="alert"></button>
        <span>
            @Html.ValidationSummary(false)
        </span>
</div>

And with using jQuery ; 并使用jQuery ;

jQuery(document).ready(function () {
   @if(!ViewData.ModelState.IsValid) {
       <text>
  $('.alert span').parent().removeClass("display-hide");
       </text>
   }
   else
   {
       <text>
  $('.alert span').parent().addClass("display-hide");
       </text>
   }
});

You can use this trick for any html element. 您可以将此技巧用于任何html元素。

on and off methods are not for showing/hiding elements but for adding and removing event listeners. onoff方法不是用于显示/隐藏元素,而是用于添加和删除事件侦听器。 I think by accident your element was already hidden and that led you to believe off hides element. 我觉得意外的元素已经被隐藏,并且使你相信off皮的元素。

If you really can't use ng-hide/ng-show I suggest you use addClass and removeClass (instead of off and on ) to add/remove 'hidden' class to your element. 如果您确实不能使用ng-hide/ng-show ,建议您使用addClassremoveClass (而不是offon )向您的元素添加/删除“隐藏”类。 This class could would set the elements display to none . 此类可以将元素display设置为none

However I encourage you to show the angular code so we can help you solve this using ng-show . 但是,我鼓励您显示角度代码,以便我们可以使用ng-show帮助您解决该问题。

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

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