文章主要内容列表:
1、 调整图片大小,不走形(FF IE 兼容)/ 剪切图片(overflow:hidden)
2、 控制textarea区域文字数量
3、 点击显示新窗口
4、 input框自动随内容自动变长
5、 添加收藏夹
6、 设置首页
7、 Jquery + Ajax 判断用户是否存在
8、 判断email格式是否正确
9、 综合判断用户名(长度,英文字段等)
10、新闻滚动
11、 只允许输入正整数 (shopping cart 使用) 或者 正数 (正整数和正小数)
12、 转换字符串为数字
13、 判断文件格式(获得文件后缀)
14、 截取字符串
15、分割字符串
主要内容 :
1、 调整图片大小,不走形(FF IE 兼容)
// 用法
function DrawImage(ImgD,FitWidth,FitHeight){
var image=new Image();
image.src=ImgD.src;
if(image.width>0 && image.height>0){
if(image.width/image.height>= FitWidth/FitHeight){
if(image.width>FitWidth){
ImgD.width=FitWidth;
ImgD.height=(image.height*FitWidth)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
} else{
if(image.height>FitHeight){
ImgD.height=FitHeight;
ImgD.width=(image.width*FitHeight)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
}
}
}
通过 overflow:hidden进行剪切:
function clipImage(o, w, h){
var img = new Image();
img.src = o.src;
if(img.width >0 && img.height>0)
{
if(img.width/img.height >= w/h)
{
if(img.width > w)
{
o.width = (img.width*h)/img.height;
o.height = h;
//o.setAttribute("style", "marginLeft:-" + ((o.width-w)/2).toString() + "px");
$(o).css("margin-left", "-"+((o.width-w)/2).toString() + "px");
}
else
{
o.width = img.width;
o.height = img.height;
}
}
else
{
if(img.height > h)
{
o.height = (img.height*w)/img.width;
o.width = w;
//o.setAttribute("style", "margin-top:-" + ((o.height-h)/2).toString() + "px");
//$(o).css("style", "margin-top:-" + ((o.height-h)/2).toString() + "px");
$(o).css("margin-top", "-"+((o.height-h)/2).toString() + "px");
}
else
{
o.width = img.width;
o.height = img.height;
}
}
}
}
实例:
ul{list-style:none;} ul li{float:left;padding:1px;border:#ccc 1px solid;width:120px;height:120px;overflow:hidden;text-align:center;over-flow:hidden;}
2、控制textarea区域文字数量
250 characters remaining
3、点击显示新窗口
//弹窗
function g_OpenWindow(pageURL, innerWidth, innerHeight)
{
var ScreenWidth = screen.availWidth
var ScreenHeight = screen.availHeight
var StartX = (ScreenWidth - innerWidth) / 2
var StartY = (ScreenHeight - innerHeight) / 2
var wins = window.open(pageURL, 'OpenWin', 'left='+ StartX + ', top='+ StartY + ', Width=' + innerWidth +', height=' + innerHeight + ', resizable=yes, scrollbars=yes, status=no, toolbar=no, menubar=no, location=no')
wins.focus();
}
Java代码 :
不幸的是,很多浏览器会屏蔽弹出窗口,你需要手动允许后方可看到!下面是强制弹出窗口,原理就是创建一个form,通过post打开。
this is a test ! we will open the deographics's website~~
当然还有更好的办法就是
4、input框自动随内容自动变长
// input auto length
// 0
function checkLength(which) {
var maxchar=100;
//var oTextCount = document.getElementById("char");
iCount = which.value.replace(/[^u0000-u00ff]/g,"aa").length;
if(iCount<=maxchar){
//oTextCount.innerHTML = ""+ iCount+"";
which.style.border = '1px dotted #FF0000';
which.size=iCount+2;
}else{
alert('Please input letter less than '+ maxchar);
}
}
5、添加收藏夹
// addfavorite
function addfavorite(){
if (document.all){
window.external.addFavorite('http://deographics.com/','deographics');
}else if (window.sidebar){
window.sidebar.addPanel('deographics', 'http://deographics.com/', "");
}
}
6、设置首页
// setHomepage
function setHomepage(){
if(document.all){
document.body.style.behavior = 'url(#default#homepage)';
document.body.setHomePage('http://deographics.com/');
}else if(window.sidebar){
if(window.netscape){
try{
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
}catch(e){
alert(" The operation was refused by browser,if you want to enable this feature, please enter in the address column 'about:config', then, change 'signed.applets.codebase_principal_support' to 'true' ");
}
}
var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
prefs.setCharPref('browser.startup.homepage','http://deographics.com/');
}
}
7、Jquery + Ajax 判断用户是否存在
// 检测 用户名是否存在
$("#username").blur(function(){
var name = $(this).val(); var table = $(this).attr('title');
if(name!=''){
var dataString = 'username='+ name + '&table=' + table;
$.post("operate.php",dataString,function(data){ //alert(data);
if(data==0){
alert('This username already exist !'); $(this).val('').focus(); return false;
}
});
}
});
或者
var datastring = 'id=' + $id + '&pos=' + $pos;
$.ajax({
type: "POST",
url: url,
data: dataString,
beforeSend: function(){},
error: function(){alert('Send Error !');},
success: function(data) {
// do something
}
});
8、判断email格式是否正确
function chekemail(temail){
var pattern = /^[w]{1}[w.-_]*@[w]{1}[w-_.]*.[w]{2,4}$/i;
if(pattern.test(temail)){return true;}else{return false;}
}
9、综合判断用户名(长度,英文字段等)
// 实例
var username = $('#username');
var backValue = VerifyInput('username');
if(backValue == 'length'){
alert("Username is make up of 3 - 15 characters!");
username.focus();
return false;
}else if(backValue == 'first'){
alert("the First character must be letter or number !");
username.focus();
return false;
}else if(backValue == 'back'){
alert("Username only contains letter number or '_' ");
username.focus();
return false;
}
// 判断
function VerifyInput(user){
var strUserID = $('#'+user).val();
if (strUserID.length < 3 || strUserID.length > 15){
return 'length';
}else{
for (nIndex=0; nIndex
10、新闻滚动
ul,li{margin:0;padding:0;font-size:12px;color:#999;}
ul{height:100px;overflow:hidden;}
ul li{line-height:20px;height:20px;}
- New York web design Inc.1
- Your site will be structured 2
- hat will communicate the 3
- hat will communicate the 4
- hat will communicate the 5
- hat will communicate the 6
- hat will communicate the 7
- hat will communicate the 8
- hat will communicate the 9
- New York web design Inc. 10
- New York web design Inc.11
- New York web design Inc. 12
- New York web design Inc. 13
- New York web design Inc. 14
Java代码
// 用法 : 四个参数分别是:操作对象, 停留时间,相对速度(越小越快),每次滚动多少(最好和Li的Line-height一致)。
scroll('news', 3000, 1, 20 );
function scroll(element, delay, speed, lineHeight) {
var numpergroup = 5;
var slideBox = (typeof element == 'string')?document.getElementById(element):element;
var delay = delay||1000;
var speed=speed||20;
var lineHeight = lineHeight||20;
var tid = null, pause = false;
var liLength = slideBox.getElementsByTagName('li').length;
var lack = numpergroup-liLength%numpergroup;
for(i=0;i
11、只允许输入正整数 (shopping cart 使用)
或正数
只能输入数字和小数点的文本框:
12、 转换字符串为数字
parseInt("1234blue"); //returns 1234
parseInt("0xA"); //returns 10
parseInt("22.5"); //returns 22
parseInt("blue"); //returns NaN
parseFloat("1234blue"); //returns 1234.0
parseFloat("0xA"); //returns NaN
parseFloat("22.5"); //returns 22.5
parseFloat("22.34.5"); //returns 22.34
parseFloat("0908"); //returns 908
parseFloat("blue"); //returns NaN
Boolean(""); //false – empty string
Boolean("hi"); //true – non-empty string
Boolean(100); //true – non-zero number
Boolean(null); //false - null
Boolean(0); //false - zero
Boolean(new Object()); //true – object
Number(false) 0
Number(true) 1
Number(undefined) NaN
Number(null) 0
Number( "5.5 ") 5.5
Number( "56 ") 56
Number( "5.6.7 ") NaN
Number(new Object()) NaN
Number(100) 100
var s1 = String(null); //"null"
var onull = null;
var s2 = oNull.toString(); //won't work, causes an error
13、 判断文件格式(获得文件后缀)
// 用法:get_ext(this,'img');
function get_ext(name){
var pos = name.lastIndexOf('.');
var extname = name.substring(pos,name.length) // like: str.split('.')
var lastname = extname.toLowerCase();
if (lastname !='.jpg' && lastname !='.gif' && lastname !='.png' && lastname !='.bmp'){
return lastname;
}else{
return name;
}
}
}
14、截取字符串
// 简单型
// 结果是 lo worl
// 复杂型(中文或者中英文混合截取)
15、分割字符串
以上就是小编为大家整理的常用的javascript函数,希望对大家的学习有所帮助,之后还有更多javascript常用函数分享给大家,继续关注。



