首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在ionic 3中包含jquery插件

问如何在ionic 3中包含jquery插件
EN

Stack Overflow用户
提问于 2018-01-24 00:17:55
回答 2查看 2.1K关注 0票数 1

我正在尝试在我的离子应用程序-> https://github.com/m10l/FloatLabel.js/tree/master中实现jQuery FloatLabel

我使用这个插件是因为这个插件也是在web版本中实现的,所以在设计上是统一的。

我将其包含在jquery的src/index.html中

代码语言:javascript
复制
<link href="assets/js/jquery.FloatLabel/jquery.FloatLabel.css" rel="stylesheet">
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="assets/js/jquery.FloatLabel/jquery.FloatLabel.js"></script>

我想在页面加载时执行此脚本

代码语言:javascript
复制
$( '.js-float-label-wrapper' ).FloatLabel();

下面是我的src/index.html代码

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
  <meta charset="UTF-8">
  <title>Ionic App</title>
  <meta name="viewport" content="viewport-fit=cover, width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
  <meta name="format-detection" content="telephone=no">
  <meta name="msapplication-tap-highlight" content="no">

  <link rel="icon" type="image/x-icon" href="assets/icon/favicon.ico">
  <link rel="manifest" href="manifest.json">
  <link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet">
  <meta name="theme-color" content="#4e8ef7">

  <!-- add to homescreen for ios -->
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">

  <!-- cordova.js required for cordova apps -->
  <script src="cordova.js"></script>

  <link href="build/main.css" rel="stylesheet">

  <link href="assets/js/jquery.FloatLabel/jquery.FloatLabel.css" rel="stylesheet">
  <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
  <script src="assets/js/jquery.FloatLabel/jquery.FloatLabel.js"></script>

  <script type="text/javascript">
    $( '.js-float-label-wrapper' ).FloatLabel();
  </script>

</head>
<body>

  <!-- Ionic's root component and where the app will load -->
  <ion-app></ion-app>

  <!-- The polyfills js is generated during the build process -->
  <script src="build/polyfills.js"></script>

  <!-- The vendor js is generated during the build process
       It contains all of the dependencies in node_modules -->
  <script src="build/vendor.js"></script>

  <!-- The main bundle js is generated during the build process -->
  <script src="build/main.js"></script>

</body>
</html>

它适用于web版本,但不适用于ionic。我如何在ionic上实现它?

我想要像这样的小提琴-> https://jsfiddle.net/acrmktq3/

EN

回答 2

Stack Overflow用户

发布于 2018-01-26 22:14:50

链接index.html中的jquery.js:

代码语言:javascript
复制
  <script src="assets/js/jquery-3.2.1.min.js"></script>

在节点中运行:

代码语言:javascript
复制
npm install jquery

并将其导入您的页面,如下所示:

代码语言:javascript
复制
import * as $ from "jquery";
票数 0
EN

Stack Overflow用户

发布于 2018-01-24 02:14:40

尝试将JavaScript代码添加到身体底部,如下所示:

代码语言:javascript
复制
<body>

  <!-- Ionic's root component and where the app will load -->
  <ion-app></ion-app>

  <!-- The polyfills js is generated during the build process -->
  <script src="build/polyfills.js"></script>

  <!-- The vendor js is generated during the build process
       It contains all of the dependencies in node_modules -->
  <script src="build/vendor.js"></script>

  <!-- The main bundle js is generated during the build process -->
  <script src="build/main.js"></script>

  <script type="text/javascript">
    $( '.js-float-label-wrapper' ).FloatLabel();
  </script>
</body>

您在应用程序初始化之前添加了它,main.js甚至还没有加载,并且您已经执行了代码,所以在代码底部使用它应该可以解决问题。

另外,你有没有尝试过在离子应用程序中使用这个FloatLabel?由于页面可能没有被加载,并且代码已经被执行,所以你需要它更多的“角度方式”。您可以在具有浮动标签的组件中使用它,只需使用它就可以在您的组件中使用JQuery:

代码语言:javascript
复制
declare var $: any; // declare this bellow your page imports to be able to use jQuery

或者你可以尝试离子浮动标签,这样就不需要在你的项目中有JQuery和其他库,即使它不是“统一”的设计

代码语言:javascript
复制
<ion-item>
  <ion-label color="primary" floating>Floating Label</ion-label>
  <!-- Use the floating attribute to have your label to float when you focus on the input -->
  <ion-input></ion-input>
</ion-item>

希望这能有所帮助。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48406306

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档