dot net core 项目,在 site.js 中使用 jquery maxlength 和 count 插件

Posted

技术标签:

【中文标题】dot net core 项目,在 site.js 中使用 jquery maxlength 和 count 插件【英文标题】:dot net core project, using jquery maxlength and count plugins inside site.js 【发布时间】:2021-02-10 08:01:32 【问题描述】:

我想知道我是否做得对。我下载了一个 jquery 插件并将其放在 lib 文件夹中。然后我在 _layout.cshtml 中写下插件的路径名,就像其他脚本一样。然后我进入 site.js 编写一些客户端代码,希望使用我的新插件,它说“$(.....)maxlength 不是一个函数”。所以我认为我的 site.js 文件无权访问我的插件。我在这里想念什么?我是否应该在其他地方也添加插件,以便 site.js 可以使用它们,对我来说没有意义。

在_layout.cshtml里面:

  <script src="~/lib/jquery/dist/jquery.min.js"></script>
  <script src="~/lib/jQuery.Maxlength-master/jquery.maxlength.min.js"> 
    </script>
  <script src="http://code.jquery.com/jquery-1.5.js"></script>
  <script src="~/js/site.js" asp-append-version="true"></script>

  @RenderSection("Scripts", required: false)
</body>
</html>

在 site.js 中只是一些尝试使用新插件的代码:

$("input").maxlength();

【问题讨论】:

【参考方案1】:

因为这个 jquery jquery.min.jsjquery-1.5.js 重复。所以,你可以删除这个脚本。

<script src="~/lib/jquery/dist/jquery.min.js"></script>

他们的顺序应该改变。

<script src="https://code.jquery.com/jquery-1.5.js"></script>
<script src="~/lib/jQuery.Maxlength-master/jquery.maxlength.min.js"></script>
<script src="~/js/site.js" asp-append-version="true"></script>
@RenderSection("Scripts", required: false)

在 Index.cshtml 中,添加一个文本框。

<input type="text" onchange="change()" maxlength="30"/>

在 site.js 中。

function change() 
 console.log($("input").maxlength())

结果

【讨论】:

以上是关于dot net core 项目,在 site.js 中使用 jquery maxlength 和 count 插件的主要内容,如果未能解决你的问题,请参考以下文章

我在 asp dot net core 中创建控制器但出现错误

2. Note Dot Net Core CMD

如何在 Dot Net Core 中使用 JWKS 验证 JWT 令牌

Angular + core 如何将文件数组从 Angular 上传到 Dot Net Core

Dot Net Core dll 作为带有事件的 COM dll

AWS Lambda Dot Net Core 3.1 在使用 Aspose Cells for .Net 时抛出 gdip 异常