您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“如何利用img和div標(biāo)簽實(shí)現(xiàn)圖像下拉列表的模擬”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“如何利用img和div標(biāo)簽實(shí)現(xiàn)圖像下拉列表的模擬”吧!
dropdownlist本身不支持圖像列表,那么我們利用jquery來(lái)實(shí)現(xiàn)下拉列表的模擬。
如圖
用到的表
如下圖,因?yàn)椴皇侵攸c(diǎn),不再介紹,只要注意一點(diǎn),company.id為自增int,注意屬性窗口中auto Generated Value為true,Auto-Sync為OnInsert,這樣linq才可沒(méi)錯(cuò)
[AcceptVerbs(HttpVerbs.Post)] public ActionResult CreateCompany(string name, string brief) { string strResult = "失敗"; ServiceDataContext db = new ServiceDataContext(); Company com = new Company(); com.Name = name; com.Brief = brief; try { db.Companies.InsertOnSubmit(com); db.SubmitChanges(); strResult = "成功"; } catch { strResult = "失敗,名稱(chēng)重復(fù)?"; } return Json(strResult); }
<td> <img id="imgDefault" src='' class="imgCss" alt="" /><img
id="imgArrow" src="<%=ViewData["arrowUrl"]%>" class="DropdownCss" alt="" />
<div id="divList" class="scrolldivHidden">
<%=ViewData["imageTable"]%>div>
<%=Html.Hidden("head") %>
td>
這里selectedUrl為選擇的圖像,有默認(rèn)值,arrowUrl為下拉圖標(biāo),ViewData["imageTable"]是下拉列表框
這里用到的css
.Visible { visibility: visible; display: block; } .Hidden { visibility: hidden; display: none; } .imgCss { cursor: hand; width: 18px; height: 18px; border: 1px solid #c3c3c3; } .DropdownCss { cursor: hand; width: 9px; height: 9px; vertical-align: top; } .scrolldivHidden { position:absolute; width: 36px; height: 50px; z-index: 1; left: 0px; top: 0x; overflow-y: auto; overflow-x:hidden; display:none; } .scrolldivVisible { position:absolute; width: 36px; height: 50px; z-index: 1; left: 0px; top: 0x; overflow-y: auto; overflow-x:hidden; display:block; }
主要我們用到j(luò)query來(lái)實(shí)現(xiàn)選擇等功能
這里用到j(luò)query一個(gè)選擇框的插件
/* * Manipulation for HTML SELECT with jQuery * Created by Baldwin (http://www.dnnsun.com) * version: 1.0 (02/03/2009) * @requires jQuery v1.2 or later */ ; (function($) { /* Clear all options */ $.fn.clearSelect = function() { return this.each(function() { if (this.tagName == 'SELECT') this.options.length = 0; }); } /* Fill the options with the object array: [{'Text':'Hello','Value':'1'}]*/ $.fn.fillSelect = function(data) { return this.clearSelect().each(function() { if (this.tagName == 'SELECT') { var dropdownList = this; if (data && data.length > 0) { $.each(data, function(index, optionData) { var option = new Option(optionData.Text, optionData.Value); if ($.browser.msie) dropdownList.add(option); else dropdownList.add(option, null); }); dropdownList.disabled = false; } else dropdownList.disabled = true; } }); } /* loading when applying Ajax fillSelect */ $.fn.loadSelect = function(loadText) { var data = [{ 'Text': loadText, 'Value': ''}]; this.fillSelect(data); } /* selected the target option with value */ $.fn.selected = function(value) { return this.each(function() { if (this.tagName == 'SELECT') { var options = this.options; if (options.length > 0) { $.each(options, function(index, optionData) { // once match then exist loop if (optionData.value == value) { optionData.selected = true; return false; } }); } } }); } /* TODO:selected the target option with text */ $.fn.selectedText = function(text) { return this.each(function() { if (this.tagName == 'SELECT') { var options = this.options; if (options.length > 0) { $.each(options, function(index, optionData) { // once match then exist loop if (optionData.text == text) { optionData.selected = true; return false; } }); } } }); } /* returns the selected value */ $.fn.getSelected = function() { return $(this).val(); } /* return the text of selected option */ $.fn.getSelectedText = function() { return $(this).children("[@selected]").text(); } })(jQuery); function getDepartment(data) { $("#department").fillSelect(data); } $("#company").change(function() { $.getJSON("GetDepartment?company=" + $("#company").val(), null, getDepartment); });
實(shí)現(xiàn)了聯(lián)動(dòng)框
下面的代碼實(shí)現(xiàn)了下拉和選擇功能
$("#imgArrow,#imgDefault").toggle(function() { $("#divList").css("left", $("#imgDefault").css("left")); $("#divList").slideToggle("fast"); }, function() { $("#divList").css("left", $("#imgDefault").css("left")); $("#divList").slideToggle("fast"); }); $("#head").val($("#imgDefault").attr("src")); $("img:.imgList").click(function() { $("#imgDefault").attr("src", $(this).attr("src")); $("#divList").slideToggle("fast"); $("#head").val($("#imgDefault").attr("src")); }).hover(function() { $(this).removeClass().addClass("alpha"); }, (function() { $(this).removeClass(); })); $("body").click(function() { $("#divList").slideUp("fast"); $("#imgDefault").hover(function() { $(this).addClass("alpha"); }, function() { $(this).removeClass(); }); });
還有我們用到的action
public ActionResult CreateService() { ViewData["company"] = GetCompanyList(); Listblank=new List(); blank.Add(GetBlankDepartment()); ViewData["department"] = blank.ToArray(); ViewData["head"] = GetHeadList(); ViewData["selectedUrl"] = Url.Content(Head_Folder + "Head/head0.gif"); ViewData["arrowUrl"] = Url.Content(Head_Folder + "/dropdown.gif"); ViewData["imageTable"] = GetImageTable(); return View(); } [AcceptVerbs(HttpVerbs.Get)] public ActionResult GetDepartment(int? company) { SelectListItem[] items=null; if(company!=null) items = GetDepartmentList((int)company); else { SelectListItem i=GetBlankDepartment(); items=new SelectListItem[]{i}; } return Json(items); } private SelectListItem[] GetCompanyList() { ServiceDataContext db = new ServiceDataContext(); var items = from t in db.Companies select new { ID = t.ID, Name = t.Name, Brief = t.Brief }; Lists = new List(); SelectListItem b = new SelectListItem(); b.Text = "--please select company--"; b.Value = ""; s.Add(b); foreach (var i in items) { SelectListItem item = new SelectListItem(); item.Text = i.Name; item.Value = i.ID.ToString(); s.Add(item); } return s.ToArray(); } private SelectListItem[] GetDepartmentList(int nCompanyId) { ServiceDataContext db = new ServiceDataContext(); var items = from t in db.Departments where t.Company==nCompanyId select new { ID = t.ID, Name = t.Name, Brief = t.Brief }; Lists = new List(); SelectListItem b = new SelectListItem(); b.Text = "--please select department--"; b.Value = ""; s.Add(b); foreach (var i in items) { SelectListItem item = new SelectListItem(); item.Text = i.Name; item.Value = i.ID.ToString(); s.Add(item); } return s.ToArray(); } private string[] GetHeadList() { Listhead = new List(); string strServer=Url.Content("~/Content/images/Head/"); string strUrlFolder = Server.MapPath(Url.Content(Head_Folder+"Head/")); string[] fileEntries = Directory.GetFiles(strUrlFolder); FileInfo fi=null; foreach (string file in fileEntries) { fi = new FileInfo(file); if (fi.Name.ToLower().IndexOf("head")==0 && !fi.FullName.ToLower().Contains("_offline")) { head.Add(strServer+fi.Name); } } return head.ToArray(); } private string GetImageTable() { System.Text.StringBuilder sbResult = new System.Text.StringBuilder(); string[] arrImgs = GetHeadList(); foreach (string strImage in arrImgs) { string strFile=strImage.Substring(strImage.LastIndexOf("/")+1); sbResult.AppendLine(string.Format("", "img_"+strFile.Remove(strFile.LastIndexOf(".")), Url.Content(strImage) )); } string strImg=sbResult.ToString(); strImg=strImg.Remove(strImg.Length-5,5); return strImg; } }
注意紫色處,我們加了cssimgList來(lái)供jquery選擇
$("img:.imgList").click(function() { $("#imgDefault").attr("src", $(this).attr("src")); $("#divList").slideToggle("fast"); $("#head").val($("#imgDefault").attr("src")); }).hover(function() { $(this).removeClass().addClass("alpha"); }, (function() { $(this).removeClass(); }));
好了,可以試試效果了。
到此,相信大家對(duì)“如何利用img和div標(biāo)簽實(shí)現(xiàn)圖像下拉列表的模擬”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢(xún),關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。