您的位置:首页 > Web前端 > JavaScript

21个值得收藏的Javascript技巧(2)

2013-07-22 23:09 639 查看
http://developer.51cto.com/art/201307/404053_2.htm11 删除用户多选框中选择的项目下面提供的技巧,是当用户在下拉框多选项目的时候,当点删除的时候,可以一次删除它们,代码如下:
function selectBoxRemove(sourceID) {

//获得listbox的id

var src = document.getElementById(sourceID);

//循环listbox

for(var count= src.options.length-1; count >= 0; count--) {

//如果找到要删除的选项,则删除

if(src.options[count].selected == true) {

try {

src.remove(count, null);

} catch(error) {

src.remove(count);

}

}

}

}

12 Listbox中的全选和非全选如果对于指定的listbox,下面的方法可以根据用户的需要,传入true或false,分别代表是全选listbox中的所有项目还是非全选所有项目,代码如下:
function listboxSelectDeselect(listID, isSelect) {

var listbox = document.getElementById(listID);

for(var count=0; count < listbox.options.length; count++) {

listbox.options[count].selected = isSelect;

}

}

13 在Listbox中项目的上下移动下面的代码,给出了在一个listbox中如何上下移动项目
unction listbox_move(listID, direction) {

var listbox = document.getElementById(listID);

var selIndex = listbox.selectedIndex;

if(-1 == selIndex) {

alert("Please select an option to move.");

return;

}

var increment = -1;

if(direction == 'up')

increment = -1;

else

increment = 1;

if((selIndex + increment) < 0 ||

(selIndex + increment) > (listbox.options.length-1)) {

return;

}

var selValue = listbox.options[selIndex].value;

var selText = listbox.options[selIndex].text;

listbox.options[selIndex].value = listbox.options[selIndex + increment].value

listbox.options[selIndex].text = listbox.options[selIndex + increment].text

listbox.options[selIndex + increment].value = selValue;

listbox.options[selIndex + increment].text = selText;

listbox.selectedIndex = selIndex + increment;

}

//..

//..

listbox_move('countryList', 'up'); //move up the selected option

listbox_move('countryList', 'down'); //move down the selected option

14 在两个不同的Listbox中移动项目如果在两个不同的Listbox中,经常需要在左边的一个Listbox中移动项目到另外一个Listbox中去,下面是相关代码:
function listbox_moveacross(sourceID, destID) {

var src = document.getElementById(sourceID);

var dest = document.getElementById(destID);

for(var count=0; count < src.options.length; count++) {

if(src.options[count].selected == true) {

var option = src.options[count];

var newOption = document.createElement("option");

newOption.value = option.value;

newOption.text = option.text;

newOption.selected = true;

try {

dest.add(newOption, null); //Standard

src.remove(count, null);

}catch(error) {

dest.add(newOption); // IE only

src.remove(count);

}

count--;

}

}

}

//..

//..

listbox_moveacross('countryList', 'selectedCountryList');

15 快速初始化Javscript数组下面的方法,给出了一种快速初始化Javscript数组的方法,代码如下:
var numbers = [];

for(var i=1; numbers.push(i++)<100;);

//numbers = [0,1,2,3 ... 100]

使用的是数组的push方法

16 截取指定位数的小数如果要截取小数后的指定位数,可以使用toFixed方法,比如:
var num = 2.443242342;

 alert(num.toFixed(2)); // 2.44

而使用toPrecision(x)则提供指定位数的精度,这里的x是全部的位数,如:

num = 500.2349;

 result = num.toPrecision(4);//输出500.2

17 检查字符串中是否包含其他字符串下面的代码中,可以实现检查某个字符串中是否包含其他字符串。代码如下:
if (!Array.prototype.indexOf) {

Array.prototype.indexOf = function(obj, start) {

for (var i = (start || 0), j = this.length; i < j; i++) {

if (this[i] === obj) { return i; }

}

return -1;

}

}

if (!String.prototype.contains) {

String.prototype.contains = function (arg) {

return !!~this.indexOf(arg);

};

}

在上面的代码中重写了indexOf方法并定义了contains方法,使用的方法如下:
var hay = "a quick brown fox jumps over lazy dog";

var needle = "jumps";

alert(hay.contains(needle));

18 去掉Javscript数组中的重复元素下面的代码可以去掉Javascript数组中的重复元素,如下:
function removeDuplicates(arr) {

var temp = {};

for (var i = 0; i < arr.length; i++)

temp[arr[i]] = true;

var r = [];

for (var k in temp)

r.push(k);

return r;

}

//用法

var fruits = ['apple', 'orange', 'peach', 'apple', 'strawberry', 'orange'];

var uniquefruits = removeDuplicates(fruits);

//输出的 uniquefruits ['apple', 'orange', 'peach', 'strawberry'];

19 去掉String中的多余空格下面的代码会为String增加一个trim()方法,代码如下:
if (!String.prototype.trim) {

String.prototype.trim=function() {

returnthis.replace(/^\s+|\s+$/g, '');

};

}

//用法

var str = "  some string    ";

str.trim();

//输出 str = "some string"

20 Javascript中的重定向在Javascript中,可以实现重定向,方法如下:
window.location.href = "http://viralpatel.net";

21 对URL进行编码有的时候,需要对URL中的传递的进行编码,方法如下:
var myOtherUrl =

"http://example.com/index.html?url=" + encodeURIComponent(myUrl);
原文链接:http://viralpatel.net/blogs/javascript-tips-tricks/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息