jQuery,让我们对dom的操作更加便捷。由于其易用性和可扩展性,jQuer也迅速风靡全球,各种插件也是目不暇接。
我相信很多人并不能直接远离jQuery去做前端,因为它太好用了,我们以前做的东西大多基于jQuery和它的插件。而且现在Angular2的组件生态还不是很完善,我们在编写Angular的时候也许会想要用到jQuery。本篇文章就简单介绍下在Angular2中使用jQuery
如果你不知道怎么搭建Angular2开发环境,请参考这篇文章:https://www.jb51.net/article/94934.htm
环境搭好只后先跑起来,然后我们进行下面步骤
首先在index.html中引用jquery,就像我们以前做的那样
然后我们编写我们的app.component.ts
import { Component,OnInit} from '@angular/core'; declare var $:any; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent implements OnInit{ ngOnInit() { $("#title").html("<p>this is a string from jQuery html setting</p>"); } }
首先需要使用declare生命我们的jQuery,使之成为一个可用的变量,然后,我们需要导入OnInit模块并实现,我们编写的jquery代码就在这里,问中展示了我们向id为title的标签替换内容,HTML页面是这样的
<div id="title" class="title"> </div>
然后,接下来的运行效果是这样的
这就意味着我们可以在Angular2中正常使用jQuery了
接下来做个简单的jQuery插件使用的例子,首先找一个我们要使用的jQuery插件
首先在index.html 中引用
然后在我们刚才的app.component.ts中的ngOnInit中写入以下初始化插件代码
ngOnInit() { $(".card").faceCursor({}); $("#title").html("<p>this is a string from jQuery html setting</p>"); }
然后我们编写html
<div id="title" class="title"> </div> <div class="container"> <div class="card"> <img src="/UploadFiles/2021-04-02/me.jpg">css
.card { background: #fff; box-shadow: 0.5em 0 1.25em #ccc; border-radius: .3em; overflow: hidden; max-width: 20em; height: 450px; margin: 0 auto; display: block; } .title{ text-align: center; } .container { width:100%; }这些工作做了之后,我们运行下,就可以得到以下效果
备注:需要使用到jQuery的地方都要用declare声明以下,比如其他组件文件中。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年09月25日
2024年09月25日
- 纯音入心系列纯音乐《天籁古筝》1CD[MP3][331MB]
- 男女对唱典藏天碟《发烧对唱·那个季节里的歌DSD》10CD[WAV]
- 群星2010-歌林精选辑[歌林][WAV+CUE]
- TrioZimbalist-PianoTriosofWeinberg,Auerbach,Dvorak(2024)[24-44,1]wav
- 群星.1992-滚石第一流台湾歌·12王牌大车拼【滚石】【WAV+CUE】
- 林子祥.1984-创作歌集【华纳】【WAV+CUE】
- 周汤豪.2010-周汤豪【华纳】【FLAC分轨】
- Mozart-TheStringQuintets-AmadeusQuartet,CecilAronowitz(2017)[24-44,1][WAV+CUE]
- JamesWilliamsDennisIrwin-Focus(2024,Red)[24-48]FLAC
- 藤泽麻衣《空みあげて》[WAV]
- 纯音入心系列纯音乐《中国古筝经典名曲》1CD[MP3][1.3GB]
- 纯音入心系列纯音乐《古筝新奏:岁月静好与筝语,细水流年与筝同》1CD[MP3][846.9MB]
- 670《脆弱敏感小女生》[320K/MP3][27.53MB]
- 曾琳.2011-好好爱我【南方】【WAV+CUE】
- 范晓萱.1995-RAIN【福茂】【WAV+CUE】