สร้างกล่อง alert ข้อความเฉพาะตัว ด้วย jQuery และ CSS อย่างง่าย

Bookmark and Share

Javascript Code

<script language="javascript" src="js/jquery-1.2.6.min.js"></script>
<script type="text/javascript">
$(function(){
	$("span.close").click(function(){
		$(this).parents("div.myAlert").hide('fast');
	});
	$(".DmyAlert button").click(function(){
		$(this).parents("div.myAlert").hide('fast');
	});
});
function alm(msg){
	var nLeft=($(window).width()/2)-125;
	$("div.DetailAl").html(msg);	
	$("div.myAlert").css({
		position:"fixed",
		left:nLeft,
		top:200
	}).fadeIn('fast');
}
</script>

CSS Code

<style type="text/css">
.myAlert{
	width:250px;
	height:100px;
	border:2px solid #50A7D2;
	background-color:#F1F5FE;
	font-size:12px;
	display:none;
}
.HmyAlert{
	width:100%;
	float:left;
	height:20px;
	border:0px solid #50A7D2;
	background:url(bg_myalert.gif) right top no-repeat;
	font-size:12px;
	text-indent:3px;
	color:#FFFFFF;	
}
.HmyAlert span.close{
	float:right;
	display:block;
	height:20px;
	width:30px;
	cursor:pointer;
}
.HmyAlert span.iTitle{
	float:left;
	display:block;
	height:20px;
/*	background-color:#FFCC00;*/
}
.DmyAlert button{
	position:relative;
	margin-left:115px;
	margin-top:0px;
	border:1px solid #50A7D;
	background-color:#EAEAEA;
	cursor:pointer;
}
.DetailAl{
	width:95%;
	height:45px;
	margin:3px;
}
</style>

HTML Code แทรกในเท็ก body

<div class="myAlert">
<div class="HmyAlert">
<span class="iTitle">Information notice</span>
<span class="close"></span></div>
<div class="DmyAlert">
<div class="DetailAl"></div>
<button>OK</button>
</div>
</div>
<!--
ตัวอย่างการใช้งาน  alm('ข้อความที่ต้องการแสดง');
-->
<a href="javascript:alm('My Alert Text Show Here');">Test</a>

รูปสำหรับใช้ใน css

Information notice
คลิกเพื่อดูตัวอย่าง



บทความในหมวดที่่น่าสนใจ อื่นๆ jQuery Learning

25 Sep 08 jquery สคริปแรกของคุณ อ่าน 2249 25 Sep 08 ตัวอย่างการใช้งานฟังก์ชันของ jQuery ในการเรียกใช้ Class ใน CSS อ่าน 2186 02 Apr 09 สร้างรายการตัวเลือกให้กับ input text ด้วย jQuery อย่างง่าย อ่าน 2156 06 Nov 09 สร้าง swap แบนเนอร์ effect แบบ fade ด้วย jQuery อย่างง่าย อ่าน 2150 25 Sep 08 การกำหนด Selectors ด้วย jquery แบบ Basic อ่าน 2147 20 Dec 09 ประยุกต์การทำ overlay เพจ กับกล่องข้อความ ด้วย jQuery อ่าน 2079 04 Feb 09 แก้ไขข้อความแบบทันที ด้วย jQuery อ่าน 1981 25 Sep 08 รู้จักฟังก์ชันของ jQuery ในการเรียกใช้ Attribute อ่าน 1977 25 Sep 08 การกำหนด selectors ด้วย jQuery แบบลำดับขั้น ตอนที่ 1 อ่าน 1965 25 Sep 08 Event ของ jquery แบบ Basic อ่าน 1956 22 Aug 09 เรียกใช้งาน jQuery ผ่าน Google API อ่าน 1953 25 Sep 08 มารู้จัก effect ของ jquery อย่างง่าย ตอนที่ 3 อ่าน 1940 25 Sep 08 สิ่งจำเป็นก่อนการทำงานของโปรแกรม อ่าน 1916 25 Sep 08 การกำหนด selectors ด้วย jQuery แบบลำดับขั้น ตอนที่ 2 อ่าน 1912 17 Nov 09 พิมพ์ข้อมูลใน textbox เพื่อ เพิ่มรายการใน listbox ด้วย jQuery อ่าน 1909
จำนวนผู้เยี่ยมชม 157929 คน 2010 © Copyright ninenik.com. All rights reserved.