js筆記

常用軟體,程式語言的筆記和疑問
Forum rules
盡量做到:
每一個貼子都精心編輯;
每一個疑問都得到解答。
ejsoon
Site Admin
Posts: 3802
Joined: 2016 Jan 10, 22:15

js object to array

Post by ejsoon » 2018 Jun 11, 14:32

Object.keys(obj).map(function (key) { return obj[key]; });

from here.

ejsoon
Site Admin
Posts: 3802
Joined: 2016 Jan 10, 22:15

js圖片上傳轉base64

Post by ejsoon » 2018 Jul 13, 17:23

鏈接在。主要代碼:

Code: Select all

function getBase64(file) {
   var reader = new FileReader();
   reader.readAsDataURL(file);
   reader.onload = function () {
     console.log(reader.result);
   };
   reader.onerror = function (error) {
     console.log('Error: ', error);
   };
}

var file = document.querySelector('#files > input[type="file"]').files[0];
getBase64(file); // prints the base64 string

ejsoon
Site Admin
Posts: 3802
Joined: 2016 Jan 10, 22:15

js拿到form數據

Post by ejsoon » 2018 Jul 26, 11:49

之前那個對radio的取值有問題,改進如下:

Code: Select all

function getFormData(formId) {
	var formElements=document.getElementById(formId).elements;
	var postData={};
	for (var i=0; i<formElements.length; i++) {
		if (formElements[i].type=="radio") {
			if (formElements[i].checked==true) {
				postData[formElements[i].name]=formElements[i].value;
			}
		} else if (formElements[i].type!="button") {
			postData[formElements[i].name]=formElements[i].value;
		}
	}
	return postData;
}


ejsoon
Site Admin
Posts: 3802
Joined: 2016 Jan 10, 22:15

js copy DOM elements

Post by ejsoon » 2018 Aug 03, 17:27

https://www.w3schools.com/jsref/met_node_clonenode.asp

Code: Select all

var itm = document.getElementById("myList2").lastChild;
var cln = itm.cloneNode(true);

ejsoon
Site Admin
Posts: 3802
Joined: 2016 Jan 10, 22:15

js DOM 增加子元素

Post by ejsoon » 2018 Aug 08, 16:30

原生:
element.appendChild(childElement);
(還有insertAfter,insertBefore)

Jquery:
$(select).append(childElement);

ejsoon
Site Admin
Posts: 3802
Joined: 2016 Jan 10, 22:15

base64圖片格式前輟

Post by ejsoon » 2018 Oct 17, 15:31

<img src="data:image/png;base64,......(後略)" />

ejsoon
Site Admin
Posts: 3802
Joined: 2016 Jan 10, 22:15

insertRow()

Post by ejsoon » 2018 Oct 30, 14:38

table要添加<tr/>不能直接用appendChild(),要用insertRow()
https://developer.mozilla.org/en-US/doc ... /insertRow
https://www.w3schools.com/jsref/met_table_insertrow.asp

Code: Select all

				var hisplit2 = hisplit[hitemid].split(",");
				var ir = htc.querySelector("table").insertRow();
				var ic0 = ir.insertCell(0);
				var ic1 = ir.insertCell(1);
				var ic2 = ir.insertCell(2);
				ic0.innerHTML = hitemid + 1;
				ic1.innerHTML = hisplit2[0];
				ic2.innerHTML = hisplit2[1];

ejsoon
Site Admin
Posts: 3802
Joined: 2016 Jan 10, 22:15

js去掉<span>標簽

Post by ejsoon » 2018 Nov 01, 15:50

來自這裡

Code: Select all

var str = // your string here

str = str.replace(/<\/?span[^>]*>/g,"");

ejsoon
Site Admin
Posts: 3802
Joined: 2016 Jan 10, 22:15

js 禁止文字選取和鼠標右鍵

Post by ejsoon » 2018 Nov 11, 11:33

<body oncontextmenu="return false;" onselectstart="return false">

如果用css:

Code: Select all

body{
    -moz-user-select:none;/*火狐*/
    -webkit-user-select:none;/*webkit浏览器*/
    -ms-user-select:none;/*IE10*/
    -khtml-user-select:none;/*早期浏览器*/
    user-select:none;
}

Post Reply