(1)封装CSS和JS代码,使用调用的方式在前台进行调用。是开发看起来简洁和易于管理,可达到重用。
由于asp.netMVC4 框架 ,在封装js和CSS的时候,有如下规范:
using System.Web; using System.Web.Optimization; namespace Cn.Com.Farm.Controller { public class BundleConfig { // 有关 Bundling 的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkId=254725 public static void RegisterBundles(BundleCollection bundles) { bundles.Add(new ScriptBundle("~/cookie").Include( "~/js/cookie/jquery.cookie.js")); bundles.Add(new ScriptBundle("~/jquery").Include( "~/js/jquery-{version}.js")); bundles.Add(new ScriptBundle("~/Farm/jquery").Include( "~/js/jquery-{version}.js", "~/res/dialog/lhgdialog.js")); bundles.Add(new ScriptBundle("~/Farm/jqueryui").Include( "~/js/jquery-ui-{version}.js")); bundles.Add(new ScriptBundle("~/Farm/jqueryval").Include( "~/js/jquery.unobtrusive*", "~/js/jquery.validate*")); bundles.Add(new ScriptBundle("~/Farm/spacecenter").Include( "~/js/spaceenterconmmon.js")); //=========================产品js================================ #region 产品js bundles.Add(new ScriptBundle("~/js/Product/Index").Include( "~/js/jquery-{version}.js", "~/js/new.js")); bundles.Add(new ScriptBundle("~/js/Product/List").Include( "~/js/jquery-{version}.js", "~/js/new.js")); bundles.Add(new ScriptBundle("~/js/Product/Details").Include( "~/js/jquery-{version}.js", "~/js/new.js")); #endregion //=======================产品css=============================== #region 产品css bundles.Add(new StyleBundle("~/css/Product/Index").Include( "~/css/css/center.css", "~/css/css/productsmenu.css", "~/css/css/copyright.css", "~/css/css/head_style.css")); bundles.Add(new StyleBundle("~/css/Product/List").Include( "~/css/css/center.css", "~/css/css/productsmenu.css", "~/css/css/copyright.css", "~/css/css/head_style.css", "~/css/page.css")); bundles.Add(new StyleBundle("~/css/Product/Details").Include( "~/css/css/head_style.css", "~/css/css/center.css", "~/css/css/copyright.css", "~/css/css/index.css", "~/css/css/css.css")); #endregion bundles.Add(new StyleBundle("~/Farm/style").Include( "~/css/css/style.css")); bundles.Add(new StyleBundle("~/css/init").Include( "~/css/init.css")); bundles.Add(new ScriptBundle("~/Farm/modernizr").Include( "~/js/modernizr-*")); bundles.Add(new StyleBundle("~/css/page").Include("~/css/page.css")); bundles.Add(new StyleBundle("~/newcss/Zone").Include( "~/css/init.css", "~/css/css/head_style.css", "~/css/css/copyright.css", "~/css/css/tree_style.css", "~/css/css/style.css" )); bundles.Add(new StyleBundle("~/newcss/Species").Include( "~/css/css/center.css", "~/css/css/copyright.css", "~/css/css/head_style.css", "~/css/css/tree_style.css" )); //========================列表页面,增加/编辑/详细[css]========================= bundles.Add(new StyleBundle("~/css/lisgerIndex").Include( "~/res/lib/ligerUI/skins/Aqua/css/ligerui-all.css", "~/res/lib/ligerUI/skins/Gray/css/all.css", "~/res/lib/ligerUI/skins/ligerui-icons.css", "~/res/lib/css/index.css" )); //==========================列表页面[js]========================= bundles.Add(new ScriptBundle("~/js/ligerlist").Include( "~/js/jquery-{version}.js", "~/res/lib/js/ligerui.all.js", "~/res/lib/js/common.js", "~/res/lib/js/LG.js", "~/res/lib/js/ligerui.expand.js", "~/res/lib/json2.js", "~/res/lib/ligerDialog.js", "~/res/lib/ligerTextBox.js", "~/res/lib/ligerCheckBox.js", "~/res/lib/ligerComboBox.js", "~/res/lib/ligerGrid.js", "~/res/lib/ligerDateEditor.js", "~/res/lib/ligerSpinner.js", "~/res/lib/ligerUI/js/plugins/ligerDrag.js", "~/res/lib/ligerUI/js/plugins/ligerMenu.js", "~/res/lib/ligerUI/js/plugins/ligerMenuBar.js", "~/res/lib/ligerUI/js/plugins/ligerToolBar.js" )); //=====================图片展示=========================== bundles.Add(new StyleBundle("~/CSS/PicShow").Include( "~/res/pic/Picshow.css")); bundles.Add(new ScriptBundle("~/JS/PicShow").Include( "~/res/pic/Picshow.js")); #region ligerUI bundles.Add(new ScriptBundle("~/ligerUI/JS/tree").Include( "~/res/ligerUI/js/core/base.js", "~/res/ligerUI/js/plugins/ligerTree.js", "~/res/ligerUI/js/plugins/ligerMenu.js", "~/res/ligerUI/js/plugins/ligerDialog.js", "~/res/ligerUI/js/plugins/ligerGrid.js" )); bundles.Add(new ScriptBundle("~/ligerUI/JS/treetopic").Include( "~/res/ligerUI/js/core/base.js", "~/res/ligerUI/js/plugins/ligerTreetopic.js", "~/res/ligerUI/js/plugins/ligerMenu.js", "~/res/ligerUI/js/plugins/ligerDialog.js", "~/res/ligerUI/js/plugins/ligerGrid.js" )); bundles.Add(new StyleBundle("~/ligerUI/CSS/tree").Include( "~/res/ligerUI/skins/Aqua/css/ligerui-all.css", "~/res/ligerUI/skins/Gray/css/dialog.css", "~/res/ligerUI/skins/ligerui-init.css")); #endregion #region Kissy #region ImageUpload bundles.Add(new StyleBundle("~/CSS/Kissy/ImageUpload").Include( "~/res/kissy/upload/style.css")); bundles.Add(new ScriptBundle("~/JS/Kissy/ImageUpload").Include( //"~/res/kissy/upload/index.js", //"~/res/kissy/upload/flash.js", //"~/res/kissy/upload/imageUploader.js", "~/res/kissy/upload/kissy.js" )); #endregion #endregion #region 个人会员空间 bundles.Add(new StyleBundle("~/PersonalSpace/Style").Include("~/css/personalSpace/style.css")); bundles.Add(new ScriptBundle("~/PersonalSpace/tab").Include("~/js/tab.js")); bundles.Add(new StyleBundle("~/PersonalSpace/page").Include("~/css/page.css")); bundles.Add(new ScriptBundle("~/PersonalSpace/jqueryui").Include( "~/res/jquery-ui/js/jquery-ui-1.10.1.custom.js", "~/res/jquery-ui/locale/jqueryui.datapicker.zh-cn.js" )); bundles.Add(new StyleBundle("~/css/jqueryui").Include( "~/res/jquery-ui/css/south-street/jquery-ui-1.10.3.custom.css" )); bundles.Add(new ScriptBundle("~/ns51/js").Include( "~/res/ns51jslib/ns51.base.js", "~/res/ns51jslib/ns51.ordermenu.js", "~/res/ns51jslib/ns51.popupselect.js", "~/res/ns51jslib/ns51.queryselect.js" )); bundles.Add(new StyleBundle("~/ns51/style").Include( "~/res/ns51jslib/themes/default/style.css" )); bundles.Add(new ScriptBundle("~/ns51/js/tip").Include( "~/res/ns51jslib/jquery.qtip.js" )); bundles.Add(new StyleBundle("~/ns51/style/tip").Include( "~/res/ns51jslib/themes/default/jquery.qtip.css" )); #endregion #region 乡村空间 bundles.Add(new StyleBundle("~/css/vhome").Include( "~/css/VillageHome/style.css" )); bundles.Add(new StyleBundle("~/css/vhome/main").Include( "~/css/VillageHome/css/comm.css", "~/css/VillageHome/css/cjhome.css" )); bundles.Add(new StyleBundle("~/css/vhome/page").Include( "~/css/VillageHome/css/comm.css", "~/css/VillageHome/css/cjhomestyle.css" )); bundles.Add(new StyleBundle("~/css/vcenter").Include( "~/css/VillageHome/css/comm.css", "~/css/VillageHome/css/nsglstyle.css" )); bundles.Add(new ScriptBundle("~/ns51/overflow").Include( "~/res/ns51jslib/ns51.textOverflow.js", "~/res/ns51jslib/ns51.paraOverflow.js" )); bundles.Add(new ScriptBundle("~/ns51/resize").Include("~/res/ns51jslib/jquery.ba-resize.js")); #endregion //地图用脚本 bundles.Add(new ScriptBundle("~/ns51/maps").Include("~/res/ns51jslib/ns51.maps.js")); #region kindEditor 富文本编辑器 bundles.Add(new ScriptBundle("~/Farm/kindEditor").Include( "~/js/kindeditor/kindeditor-min.js", "~/js/kindeditor/lang/zh_CN.js")); #endregion #region 日期控件 bundles.Add(new ScriptBundle("~/Farm/datePicker").Include("~/js/time/WdatePicker.js")); #endregion } } }
注释:这里是把要处理(封装的js和Css目录)bundles.Add(new function());
例如:
<!--定义下拉框的样式--> <style type="text/css"> .w_100 { /*以此来控制该下拉框的长度,可按需要添加多个长度Class,如w_100,w_200*/ width: 262px !important; height: 23px; } </style> <!--js开始--> <script language="javascript" type="text/javascript"> function chg() { if ($("#sel").val() == "New") { $("#inputGName").show(); $("#GName").hide(); // var o = document.form1.sel; } else { //if ($("#sel").val() == "Ole") $("#GName").show(); $("#inputGName").hide(); } } </script> <!--结束--
//-------------------------------------------------------------前台的调用-------------------------------------------------------------------------//
//对样式和js的封装后,前台进行调用的方法 如下:
@Scripts.Render("~/js/jquery-1.7.1.min.js") @Scripts.Render("~/js/jquery.validate.js")
@section Styles{
@Styles.Render("~/css/vhome/page")
@Styles.Render("~/css/vhome/page")
}
@section Scripts{
<script type="text/javascript">
function check(obj) { //先来个简单的验证
//投诉内容
if (obj.FReportContent.value == "") {
alert("投诉举报内容不能为空!");
obj.FReportContent.focus();
return false;
}
//姓名
if (obj.FName.value == "") {
alert("姓名不能为空!");
obj.FName.focus();
return false; //使程序不在往下执行
}
//电话号码
if (obj.FIdCard.value == "") {
alert("电话号码不能为空!");
obj.FIdCard.focus();
return false;
}
//邮箱
if (obj.FEmail.value == "") {
alert("邮箱不能为空!");
obj.FEmail.focus();
return false;
}
}
</script>
<script type="text/javascript">
window.onload = function () {
var contents = document.getElementById("content");
var FReportContent = document.form1.FReportContent;
FReportContent.onkeyup = FReportContent.onkeydown = function () {
var str = FReportContent.value;
var lengths = check(str);
var strs = 300;
if (lengths > 300) {
FReportContent.value = str.substring(0, strs)
}
contents.innerHTML = " 一共能输入" + strs + "个字符,已经输入" + lengths + "个,还剩余" + (strs - lengths) + "个";
}
function check(str) {
var num = 0
for (var i = 0; i < str.length; i++) {
if (str.charCodeAt(i) >= 0 && str.charCodeAt(i) <= 255) {
num++;
} else {
num = num + 2;
}
}
return num;
}
}
</script>
}
注释:@section Scripts{......} 里面放置js相关代码。
转载于:https://www.cnblogs.com/softmans/p/3332713.html