angular.js - angular中加jquery,选择器使用不了
PHPz
PHPz 2017-05-15 17:01:18
[AngularJS讨论组]
$(document).ready(function(){
    $(".team").hover(function(){$(".shadow").css("display","block");console.log("1")},
    function(){$(".shadow").css("display","none");console.log("0")});
 
    });
在一个angular项目里面加上面这段jquery代码,会跑不起来,把".team"改成"p"后可以使用,team是html中已经有过声明的p,在非angular项目里面用同样的代码可以跑起来。  我想知道angular中引入jquery对jquery选择器产生了影响吗? 如果有影响,为什么下面的".shadow"又能够被选择到呢?  纠结了一下午,希望大神能够解答一下。

下面是一些相关的代码:
依赖部分:
 <script src="bower_components/jquery/dist/jquery.min.js"></script>
<script src="bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="bower_components/jquery-loading/dist/jquery.loading.min.js"></script>
<!-- AngularJS dependences -->
<script src="bower_components/angular/angular.min.js"></script>
<script src="bower_components/angular-ui-router/release/angular-ui-router.min.js"></script>
html部分:    
       <p  class="team" ng-repeat="group in groups">
              显示内容省略
        <p class="shadow">
           
        <p class="content">详情</p>
        <p class="edit">编辑</p>
    </p>

</p>
js部分(把这段代码放在这个页面的控制器里面的):
$(document).ready(function($){
     $(".team").hover(function(){$(".shadow").css("display","block");console.log("1")},
    function(){$(".shadow").css("display","none");console.log("0")});

    }(jQuery));
PHPz
PHPz

学习是最好的投资!

全部回复(2)
高洛峰

在引入AngularJS之前引入jQuery,AngularJS就会使用jQuery代替自带的jqLite。
详见AngularJS文档

If jQuery is available, angular.element is an alias for the jQuery function. If jQuery is not available, angular.element delegates to Angular's built-in subset of jQuery, called "jQuery lite" or jqLite.

To use jQuery, simply ensure it is loaded before the angular.js file. You can also use the ngJq directive to specify that jqlite should be used over jQuery, or to use a specific version of jQuery if multiple versions exist on the page.

放在Angular控制器里的代码 不需要包含在$(document).ready()的回调函数里。
当控制器代码被执行时,文档的ready事件早就已经发生过了。

滿天的星座

在Angular中使用jQuery时,因为同时用到了"$"符号,所以需要将$委托给jQuery使用

$(document).ready(function($){
    $(".team").hover(function(){$(".shadow").css("display","block");console.log("1")},
    function(){$(".shadow").css("display","none");console.log("0")});
 
}(jQuery));
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号