下面是编程之家 jb51.cc 通过网络收集整理的代码片段。
编程之家小编现在分享给大家,也给大家做个参考。
- var fruits = ['apple', 'peaches', 'oranges', 'mangoes'];
- var str = fruits.valueOf();
- var fruits = ['apple', 'mangoes'];
- var str = fruits.join("|");
- var str = "apple, peaches, oranges, mangoes";
- var fruitsArray = str.split(",");
- function removeByIndex(arr, index) {
- arr.splice(index, 1);
- }
- test = new Array();
- test[0] = 'Apple';
- test[1] = 'Ball';
- test[2] = 'Cat';
- test[3] = 'Dog';
- alert("Array before removing elements: "+test);
- removeByIndex(test, 2);
- alert("Array after removing elements: "+test);
- function removeByValue(arr, val) {
- for(var i=0; i<arr.length; i++) {
- if(arr[i] == val) {
- arr.splice(i, 1);
- break;
- }
- }
- }
- var somearray = ["mon", "tue", "wed", "thur"]
- removeByValue(somearray, "tue");
- //somearray 将会有的元素是 "mon", "thur"
- Array.prototype.removeByValue = function(val) {
- for(var i=0; i<this.length; i++) {
- if(this[i] == val) {
- this.splice(i, 1);
- break;
- }
- }
- }
- //..
- var somearray = ["mon", "thur"]
- somearray.removeByValue("tue");
- var strFun = "someFunction"; //someFunction 为已经定义的方法名
- var strParam = "this is the parameter"; //要传入方法的参数
- var fn = window[strFun];
- //调用方法传入参数
- fn(strParam);
- var random = Math.floor(Math.random() * N + 1);
- //产生1到10之间的随机数
- var random = Math.floor(Math.random() * 10 + 1);
- //产生1到100之间的随机数
- var random = Math.floor(Math.random() * 100 + 1);
- <script language="javascript">
- function fnUnloadHandler() {
- alert("Unload event.. Do something to invalidate users session..");
- }
- </script>
- <body onbeforeunload="fnUnloadHandler()">
- ………
- </body>
- window.onbeforeunload = function() {
- return "You work will be lost.";
- };
- function formIsDirty(form) {
- for (var i = 0; i < form.elements.length; i++) {
- var element = form.elements[i];
- var type = element.type;
- if (type == "checkbox" || type == "radio") {
- if (element.checked != element.defaultChecked) {
- return true;
- }
- }
- else if (type == "hidden" || type == "password" ||
- type == "text" || type == "textarea") {
- if (element.value != element.defaultValue) {
- return true;
- }
- }
- else if (type == "select-one" || type == "select-multiple") {
- for (var j = 0; j < element.options.length; j++) {
- if (element.options[j].selected !=
- element.options[j].defaultSelected) {
- return true;
- }
- }
- }
- }
- return false;
- }
- window.onbeforeunload = function(e) {
- e = e || window.event;
- if (formIsDirty(document.forms["someForm"])) {
- // IE 和 Firefox
- if (e) {
- e.returnValue = "You have unsaved changes.";
- }
- // Safari浏览器
- return "You have unsaved changes.";
- }
- };
- <SCRIPT type="text/javascript">
- window.history.forward();
- function noBack() { window.history.forward(); }
- </SCRIPT>
- </HEAD>
- <BODY onload="noBack();"
- onpageshow="if (event.persisted) noBack();" onunload="">
- 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);
- }
- }
- }
- }
- function listboxSelectDeselect(listID, isSelect) {
- var listbox = document.getElementById(listID);
- for(var count=0; count < listbox.options.length; count++) {
- listbox.options[count].selected = isSelect;
- }
- }
- 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
- 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');
- var numbers = [];
- for(var i=1; numbers.push(i++)<100;);
- //numbers = [0,1,2,3 ... 100]
- 使用的是数组的push方法
- var num = 2.443242342;
- alert(num.toFixed(2)); // 2.44
- 而使用toPrecision(x)则提供指定位数的精度,这里的x是全部的位数,如:
- num = 500.2349;
- result = num.toPrecision(4);//输出500.2
- 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);
- };
- }
- var hay = "a quick brown fox jumps over lazy dog";
- var needle = "jumps";
- alert(hay.contains(needle));
- 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', 'strawberry'];
- if (!String.prototype.trim) {
- String.prototype.trim=function() {
- return this.replace(/^\s+|\s+$/g, '');
- };
- }
- //用法
- var str = " some string ";
- str.trim();
- //输出 str = "some string"
- window.location.href = "http://viralpatel.net";
- var myOtherUrl =
- "http://example.com/index.html?url=" + encodeURIComponent(myUrl);
以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。