js筆記

常用軟體,程式語言的筆記和疑問
Forum rules
盡量做到:
每一個貼子都精心編輯;
每一個疑問都得到解答。
ejsoon
Site Admin
Posts: 3533
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: 3533
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: 3533
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: 3533
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: 3533
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);

Post Reply