js一堆2
js,按照比例的缩放图片.html
Code
<script language="javascript">
function changeImg(mypic){
var xw=320;
var xl=360;
var width = mypic.width;
var height = mypic.height;
var bili = width/height;
var A=xw/width;
var B=xl/height;
if(!(A>1&&B>1))
{
if(A<B)
{
mypic.width=xw;
mypic.height=xw/bili;
}
if(A>B)
{
mypic.width=xl*bili;
mypic.height=xl;
}
}
}
</script>
<img src="b.jpg" onload="changeImg(this)">
<script language="javascript">
function changeImg(mypic){
var xw=320;
var xl=360;
var width = mypic.width;
var height = mypic.height;
var bili = width/height;
var A=xw/width;
var B=xl/height;
if(!(A>1&&B>1))
{
if(A<B)
{
mypic.width=xw;
mypic.height=xw/bili;
}
if(A>B)
{
mypic.width=xl*bili;
mypic.height=xl;
}
}
}
</script>
<img src="b.jpg" onload="changeImg(this)">
js,不按照比例的缩放图片.html
Code
<script language="javascript">
function changeImg(mypic){
var xw=260;
var xl=320;
var width = mypic.width;
var height = mypic.height;
if (width > xw ) mypic.width = xw;
if (height > xl ) mypic.height = xl;
}
</script>
<img src="b.jpg" onload="changeImg(this)">
<script language="javascript">
function changeImg(mypic){
var xw=260;
var xl=320;
var width = mypic.width;
var height = mypic.height;
if (width > xw ) mypic.width = xw;
if (height > xl ) mypic.height = xl;
}
</script>
<img src="b.jpg" onload="changeImg(this)">
js,控制div滚动条滚动.aspx
Code
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
</script>
<script language="javascript">
function sc()
{
document.getElementById("div1").scrollTop+=20
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div id="div1" style="width:100px;height:100px;overflow-x:hidden;overflow-y:scroll">
AAAAAAAAAA<br>AAAAAAAAAA<br>AAAAAAAAAA<br>AAAAAAAAAA<br>AAAAAAAAAA
<br>AAAAAAAAAA<br>AAAAAAAAAA<br>AAAAAAAAAA<br>AAAAAAAAAA
<br>AAAAAAAAAA<br>AAAAAAAAAA<br>AAAAAAAAAA<br>AAAAAAAAAA<br>
AAAAAAAAAA<br>AAAAAAAAAA<br>AAAAAAAAAA<br>AAAAAAAAAA<br>
AAAAAAAAAA<br>AAAAAAAAAA<br>AAAAAAAAAA<br>AAAAAAAAAA<br>AAAAAAAAAA
<br>AAAAAAAAAA
<br>AAAAAAAAAA<br>AAAAAAAAAA<br></div>
<input type="button" onclick="sc()" value="滚动">
</form>
</body>
</html>
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
</script>
<script language="javascript">
function sc()
{
document.getElementById("div1").scrollTop+=20
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div id="div1" style="width:100px;height:100px;overflow-x:hidden;overflow-y:scroll">
AAAAAAAAAA<br>AAAAAAAAAA<br>AAAAAAAAAA<br>AAAAAAAAAA<br>AAAAAAAAAA
<br>AAAAAAAAAA<br>AAAAAAAAAA<br>AAAAAAAAAA<br>AAAAAAAAAA
<br>AAAAAAAAAA<br>AAAAAAAAAA<br>AAAAAAAAAA<br>AAAAAAAAAA<br>
AAAAAAAAAA<br>AAAAAAAAAA<br>AAAAAAAAAA<br>AAAAAAAAAA<br>
AAAAAAAAAA<br>AAAAAAAAAA<br>AAAAAAAAAA<br>AAAAAAAAAA<br>AAAAAAAAAA
<br>AAAAAAAAAA
<br>AAAAAAAAAA<br>AAAAAAAAAA<br></div>
<input type="button" onclick="sc()" value="滚动">
</form>
</body>
</html>
js,以提示方式读取文本框text.aspx
Code
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
string myScript = @"alert(document.forms[0]['TextBox1'].value);";
Page.ClientScript.RegisterStartupScript(this.GetType(),
"MyScript", myScript, true);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" Text="Hello ASP.NET"></asp:TextBox>
</div>
</form>
</body>
</html>
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
string myScript = @"alert(document.forms[0]['TextBox1'].value);";
Page.ClientScript.RegisterStartupScript(this.GetType(),
"MyScript", myScript, true);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" Text="Hello ASP.NET"></asp:TextBox>
</div>
</form>
</body>
</html>
js,在page_load中添加js方法.aspx
Code
<%@ Page Language="C#" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
string myScript = @"function AlertHello() { alert('Hello ASP.NET'); }";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MyScript", myScript, true);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Adding JavaScript0</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" Runat="server" Text="Button"
OnClientClick="AlertHello()" />
</div>
</form>
</body>
</html>
<%@ Page Language="C#" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
string myScript = @"function AlertHello() { alert('Hello ASP.NET'); }";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MyScript", myScript, true);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Adding JavaScript0</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" Runat="server" Text="Button"
OnClientClick="AlertHello()" />
</div>
</form>
</body>
</html>
js,给textbox赋值,值为当前时间.aspx
Code
<%@ Page Language="C#" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Using JavaScript</title>
</head>
<body onload="javascript:document.forms[0]['TextBox1'].value=Date();">
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" Runat="server" Width="300"></asp:TextBox>
</div>
</form>
</body>
</html>
<%@ Page Language="C#" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Using JavaScript</title>
</head>
<body onload="javascript:document.forms[0]['TextBox1'].value=Date();">
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" Runat="server" Width="300"></asp:TextBox>
</div>
</form>
</body>
</html>
js,向数据库中插入数据.aspx
Code
<%@page language="C#"%>
<%@import namespace="System.Data"%>
<%@import namespace="System.Data.OleDb"%>
<html>
<script language="C#" runat="server">
void page_load(object serder, EventArgs e)
{
}
void intobase(object serder, EventArgs e)
{
OleDbConnection oleconnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("App_Data/test.mdb"));
OleDbCommand olecommand=new OleDbCommand();
oleconnection.Open();
olecommand.Connection=oleconnection;
olecommand.CommandText = "INSERT INTO student(name) VALUES('" + TextBox1.Text + "')";
olecommand.ExecuteNonQuery();
olecommand=null;
oleconnection.Close();
oleconnection=null;
Label1.Text = "录入成功";
}
</script>
<body>
<form id="Form1" runat=server>
<br>
输入一些内容:<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><BR>
<asp:button runat=server id=inbase text="提交" onclick="intobase"></asp:Button>
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
<br />
可以做成文章提交系统。
</form>
</body>
</html>
<%@page language="C#"%>
<%@import namespace="System.Data"%>
<%@import namespace="System.Data.OleDb"%>
<html>
<script language="C#" runat="server">
void page_load(object serder, EventArgs e)
{
}
void intobase(object serder, EventArgs e)
{
OleDbConnection oleconnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("App_Data/test.mdb"));
OleDbCommand olecommand=new OleDbCommand();
oleconnection.Open();
olecommand.Connection=oleconnection;
olecommand.CommandText = "INSERT INTO student(name) VALUES('" + TextBox1.Text + "')";
olecommand.ExecuteNonQuery();
olecommand=null;
oleconnection.Close();
oleconnection=null;
Label1.Text = "录入成功";
}
</script>
<body>
<form id="Form1" runat=server>
<br>
输入一些内容:<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><BR>
<asp:button runat=server id=inbase text="提交" onclick="intobase"></asp:Button>
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
<br />
可以做成文章提交系统。
</form>
</body>
</html>
js,鼠标随意拖拽网页中图片或者表格等元素.html
Code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Untitled Page</title>
<script type="text/javascript" language="javascript">
//为Number增加一个属性,判断当前数据类型是否是数字
Number.prototype.NaN0=function(){return isNaN(this)?0:this;}
//全局变量
var iMouseDown=false;
var dragObject=null;
//获得鼠标的偏移量(对象2-对象1)
function getMouseOffset(target,ev)
{
ev=ev||window.event;
var docPos=getPosition(target);
var mousePos=mouseCoords(ev);
return {x:mousePos.x-docPos.x,y:mousePos.y-docPos.y};
}
//获得事件发生的实际位置----------------------对象1
function getPosition(e)
{
var left=0;
var top=0;
//相对位置累加得到实际位置
while(e.offsetParent)
{
left+=e.offsetLeft+(e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)).NaN0():0);
top+=e.offsetTop+(e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)).NaN0():0);
e=e.offsetParent;
}
left+=e.offsetLeft+(e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)).NaN0():0);
top+=e.offsetTop+(e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)).NaN0():0);
return {x:left,y:top};
}
//获得发生事件鼠标的位置----------------------对象2
function mouseCoords(ev)
{
if(ev.pageX||ev.pageY)
{
return {x:ev.pageX,y:ev.pageY};
}
return {x:ev.clientX+document.body.scrollLeft-document.body.clientLeft,y:ev.clientY+document.body.scrollTop-document.body.clientTop};
}
//定义可以拖拽的对象
function makeDragable(item)
{
if(!item) return;
//为可拖拽对象定义一个onmousedown事件的方法
ev=window.event;
item.onmousedown=function(ev)
{
dragObject=this;
mouseOffset=getMouseOffset(this,ev);
return false;
}
}
//定义鼠标点下所调用的方法
function mouseDown(ev)
{
ev=ev||window.event;
var target=ev.target||ev.srcElement;
if(target.onmousedown||target.getAttribute('DragObj'))
{
return false;
}
}
//鼠标抬起后释放对象
function mouseUp(ev)
{
dragObject = null;
//onmouseup事件触发时说明鼠标已经松开,所以设置down变量值为false
iMouseDown = false;
}
//鼠标移动
function mouseMove(ev)
{
ev=ev||window.event;
var target = ev.target || ev.srcElement;
var mousePos = mouseCoords(ev);
if(dragObject)
{
if(dragObject.style)
{
//移动对象
dragObject.style.left=mousePos.x - mouseOffset.x;
dragObject.style.top= mousePos.y - mouseOffset.y;
}
}
//lMouseState = iMouseDown;
if(dragObject) return false;
}
document.onmousedown=mouseDown;
document.onmousemove=mouseMove;
document.onmouseup=mouseUp;
function moveImg()
{
var img1=document.getElementById('img1');
makeDragable(img1);
}
</script>
</head>
<body onload="moveImg()">
<div>
<img src="http://images.163.com/homepage/logo.gif" alt="" id="img1" style="position:absolute;left:0px;top:0px;" />
</div>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Untitled Page</title>
<script type="text/javascript" language="javascript">
//为Number增加一个属性,判断当前数据类型是否是数字
Number.prototype.NaN0=function(){return isNaN(this)?0:this;}
//全局变量
var iMouseDown=false;
var dragObject=null;
//获得鼠标的偏移量(对象2-对象1)
function getMouseOffset(target,ev)
{
ev=ev||window.event;
var docPos=getPosition(target);
var mousePos=mouseCoords(ev);
return {x:mousePos.x-docPos.x,y:mousePos.y-docPos.y};
}
//获得事件发生的实际位置----------------------对象1
function getPosition(e)
{
var left=0;
var top=0;
//相对位置累加得到实际位置
while(e.offsetParent)
{
left+=e.offsetLeft+(e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)).NaN0():0);
top+=e.offsetTop+(e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)).NaN0():0);
e=e.offsetParent;
}
left+=e.offsetLeft+(e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)).NaN0():0);
top+=e.offsetTop+(e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)).NaN0():0);
return {x:left,y:top};
}
//获得发生事件鼠标的位置----------------------对象2
function mouseCoords(ev)
{
if(ev.pageX||ev.pageY)
{
return {x:ev.pageX,y:ev.pageY};
}
return {x:ev.clientX+document.body.scrollLeft-document.body.clientLeft,y:ev.clientY+document.body.scrollTop-document.body.clientTop};
}
//定义可以拖拽的对象
function makeDragable(item)
{
if(!item) return;
//为可拖拽对象定义一个onmousedown事件的方法
ev=window.event;
item.onmousedown=function(ev)
{
dragObject=this;
mouseOffset=getMouseOffset(this,ev);
return false;
}
}
//定义鼠标点下所调用的方法
function mouseDown(ev)
{
ev=ev||window.event;
var target=ev.target||ev.srcElement;
if(target.onmousedown||target.getAttribute('DragObj'))
{
return false;
}
}
//鼠标抬起后释放对象
function mouseUp(ev)
{
dragObject = null;
//onmouseup事件触发时说明鼠标已经松开,所以设置down变量值为false
iMouseDown = false;
}
//鼠标移动
function mouseMove(ev)
{
ev=ev||window.event;
var target = ev.target || ev.srcElement;
var mousePos = mouseCoords(ev);
if(dragObject)
{
if(dragObject.style)
{
//移动对象
dragObject.style.left=mousePos.x - mouseOffset.x;
dragObject.style.top= mousePos.y - mouseOffset.y;
}
}
//lMouseState = iMouseDown;
if(dragObject) return false;
}
document.onmousedown=mouseDown;
document.onmousemove=mouseMove;
document.onmouseup=mouseUp;
function moveImg()
{
var img1=document.getElementById('img1');
makeDragable(img1);
}
</script>
</head>
<body onload="moveImg()">
<div>
<img src="http://images.163.com/homepage/logo.gif" alt="" id="img1" style="position:absolute;left:0px;top:0px;" />
</div>
</body>
</html>