如何在 Orchard CMS 中使用 javascript(js 文件)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7142925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use javascript(js file) in Orchard CMS
提问by user518363
Can any one tell me please how to use js file in Orchard CMS? I added it to Layout.cshtml page as
Script.include("jquery.js")
.
谁能告诉我如何在Orchard CMS中使用js文件?我将它添加到 Layout.cshtml 页面作为
Script.include("jquery.js")
.
回答by Piotr Szmyd
You can do it twofold.
你可以双管齐下。
First approach is just like mdm described:
第一种方法就像 mdm 描述的那样:
- Create your own implementation of
IResourceManifestProvider
interface (take a look at Orchard source - it's been implemented in many modules), implementBuildManifests(ResourceManifestBuilder builder)
method and create a named resource for a given .js file - Use
@{ Script.Require("[your resource name]"); }
in your Razor view file (.cshtml) to include that script.
- 创建您自己的
IResourceManifestProvider
接口实现(查看 Orchard 源代码 - 它已在许多模块中实现),实现BuildManifests(ResourceManifestBuilder builder)
方法并为给定的 .js 文件创建命名资源 - 使用
@{ Script.Require("[your resource name]"); }
您的Razor视图文件(.cshtml)以包括脚本。
This is a preferred solution if you have many script files, possibly with dependencies between them. It allows you to specify the dependencies for each script file and make Orchard take care of the rest (so when you reference a given resource, all dependent ones would also be automatically referenced in the proper order).
如果您有许多脚本文件,并且它们之间可能存在依赖关系,则这是首选解决方案。它允许您为每个脚本文件指定依赖项,并使 Orchard 负责其余部分(因此,当您引用给定资源时,所有依赖的资源也将按正确的顺序自动引用)。
The second, simpler approachis to directly reference the .js script file in your .cshtml file, without creating a named resource. It's useful if you'd like to quickly add a reference to a single script. Like this (example taken from Orchard.Web\Core\Shapes\Views\Document.cshtml):
第二种更简单的方法是直接引用 .cshtml 文件中的 .js 脚本文件,而不创建命名资源。如果您想快速添加对单个脚本的引用,它会很有用。像这样(示例取自 Orchard.Web\Core\Shapes\Views\Document.cshtml):
@{ Script.Include("html5.js").AtLocation(ResourceLocation.Head); }
回答by mdm
If you have the Orchard.jQuery module installed and enabled, then at the top of your view use:
如果您安装并启用了 Orchard.jQuery 模块,则在视图顶部使用:
Script.Require("jQuery")
Have a look at the ResourceManifest class in the Orchard.jQuery project (should be somewhere in your Orchard solution file), there you can see all the different jQuery modules you can include using this syntax (e.g. jQueryUI_Core
, jQueryUI_Tabs
, etc)
看一看在Orchard.jQuery项目ResourceManifest类(应该在你的果园解决方案文件的地方),在那里,你可以看到所有的不同的jQuery的模块,您可以使用此语法包含(例如jQueryUI_Core
,jQueryUI_Tabs
等)
E.g.
例如
manifest.DefineScript("jQueryUI").SetUrl("jquery-ui.min.js", "jquery-ui.js").SetVersion("1.9.2").SetDependencies("jQuery")
.SetCdn("//ajax.aspnetcdn.com/ajax/jquery.ui/1.9.2/jquery-ui.min.js", "//ajax.aspnetcdn.com/ajax/jquery.ui/1.9.2/jquery-ui.js", true);
回答by DanielEli
It's kinda cheating but you can just add this to the layout or document file.
这有点作弊,但您可以将其添加到布局或文档文件中。
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.11/jquery-ui.min.js" type="text/javascript"></script>
回答by Sriwantha Attanayake
To have a good example look for the module ContentPicker. This comes with default Orchard source code. It shows how you can use Script.Require("MyScript"). JQuery module itself is a good example. This module also comes with default source code.
要获得一个好的示例,请查找 ContentPicker 模块。这带有默认的 Orchard 源代码。它展示了如何使用 Script.Require("MyScript")。JQuery 模块本身就是一个很好的例子。该模块还带有默认源代码。
Note: I am referring to Orchard 1.7 and later versions
注意:我指的是 Orchard 1.7 及更高版本