- Object.assign()
=> MDN
Object.assign() //方法用于將所有可枚舉屬性的值從一個(gè)或多個(gè)源對(duì)象復(fù)制到目標(biāo)對(duì)象夷都。它將返回目標(biāo)對(duì)象诡挂。
------------
let obj1 = {
a: 1,
b: 2,
c: 3
}
let obj2 = Object.assign({ d: 4, e: 5 }, obj1)
console.log(obj2.d) //4
- String.trim()
=> MDN
String.prototype.trim() //方法會(huì)從一個(gè)字符串的兩端刪除空白字符。在這個(gè)上下文中的空白字符是所有的空白字符 (space, tab, no-break space 等) 以及所有行終止符字符(如 LF碘橘,CR)
-----------------------------
var orig = ' foo ';
console.log(orig.trim()); // 'foo'
- 獲取select下拉選擇框的option中的value
<select name="types" class="form-control required" style="width: 200px; border-radius: 5px;">
<option value="disabled selected hidden">請(qǐng)選擇</option>
//如果為form 表單提交 應(yīng)該刪除disabled吠撮, 它會(huì)阻止默認(rèn)選項(xiàng)的value值傳遞
<option value="1">分公司</option>
<option value="2">代理商</option>
<option value="3">客戶</option>
</select>
</form>
<button class="a">獲取</button>
<script>
$('.a').on('click',function() {
var options=$(".required > option:selected")
console.log(options.val())
})
</script>
- 字符串轉(zhuǎn)變?yōu)閚umber
var a = '123'
parseInd(a) // number 123
- 手機(jī)號(hào)正則
function checkPhone(){
var phone = document.getElementById('phone').value;
if(!(/^1[34578]\d{9}$/.test(phone))){
alert("手機(jī)號(hào)碼有誤唆缴,請(qǐng)重填");
return false;
}
}
- 正則匹配 0~1 之間的小數(shù)(包含0和1)
var re=/^(1|0(\.\d{1})?)$/
- 取消input type=“text” 的搜索記錄
autocomplete="off"
- 截取字符串 substring()
var a = "350100"
a.substring(3,6) // 從下標(biāo)3到下標(biāo)6 100
- select option 下拉列表墩邀,頁面刷新依舊為之前選擇的值
原文章地址:https://blog.csdn.net/ONEDAY_789/article/details/79961968
html:
<body onload="selectIndex();">
<form action="history.php" method="post">
<select style='width:10%;height:20%;' class='form-control' name='searchtitle' onchange='getTitleData()' type='text' id='searchtitle'>
<option value='2'>運(yùn)營(yíng)32位測(cè)試數(shù)據(jù)</option>
<option value='3'>運(yùn)營(yíng)64位測(cè)試數(shù)據(jù)</option>
<option value='4'>主干32位測(cè)試數(shù)據(jù)</option>
<option value='5'>集成32位測(cè)試數(shù)據(jù)</option>
<option value='6'>集成64位測(cè)試數(shù)據(jù)</option>
<option value='8'>主干64位測(cè)試數(shù)據(jù)</option>
</select><br>
</form>
</body>
js:
getTitleData=function(){
var searchtitle = $("#searchtitle").val();
var searchtitle = $.trim(searchtitle);
window.location = 'history.php?id=' + searchtitle;
document.cookie = "id=" + searchtitle; //將select選中的value寫入cookie中
};
selectIndex=function(){
var id = 0;
var coosStr = document.cookie; //獲取cookie中的數(shù)據(jù)
var coos=coosStr.split("; "); //多個(gè)值之間用; 分隔
for(var i=0;i<coos.length;i++){ //獲取select寫入的id
var coo=coos[i].split("=");
if("id"==coo[0]){
id=coo[1];
}
}
var stitle=document.getElementById("searchtitle");
if(stitle == 0){
stitle.selectedIndex = 0;
}
else{ //如果從cookie中獲取的id和select中的一致則設(shè)為默認(rèn)狀態(tài)
var len = stitle.options.length;
for(var i=0;i<len;i++){
if(stitle.options[i].value == id){
stitle.selectedIndex=i;
break;
}
}
}
}
- input type=checkbox ,傳遞 0粘舟,1狀態(tài)碼。
<!-- 添加一個(gè)input 當(dāng)checkbox 為true時(shí)會(huì)覆蓋此值绰疤。 為false時(shí) 則使用此值铜犬。為什么使用呢。因?yàn)閏heckbox為false時(shí)不傳值 -->
<input name="status" type="hidden" value="1" id="public">
<input type="checkbox" name="status" class="onoffswitch-checkbox" id="example7" checked>
- jQuery 遍歷 - siblings() 方法
- 自己的用途轻庆, 監(jiān)聽ul下li點(diǎn)擊事件癣猾,為其添加背景顏色
$('.xxx').on('click', 'li', function() {
$(this).siblings('li').removeClass('active') //先刪除所有l(wèi)i元素的的點(diǎn)擊的時(shí)的背景顏色
$(this).addClass('active') //給當(dāng)前 li 添加背景顏色
})
- Object.keys(obj)
- 參數(shù) obj要返回其枚舉自身屬性的對(duì)象。
// simple array
var arr = ['a', 'b', 'c'];
console.log(Object.keys(arr)); // console: ['0', '1', '2']
// array like object
var obj = { 0: 'a', 1: 'b', 2: 'c' };
console.log(Object.keys(obj)); // console: ['0', '1', '2']
// array like object with random key ordering
var anObj = { 100: 'a', 2: 'b', 7: 'c' };
console.log(Object.keys(anObj)); // console: ['2', '7', '100']
- Object.entries() 方法返回一個(gè)給定對(duì)象自身可枚舉屬性的鍵值對(duì)數(shù)組
-
Object.entries()方法返回一個(gè)給定對(duì)象自身可枚舉屬性的鍵值對(duì)數(shù)組余爆,其排列與使用
for...in
循環(huán)遍歷該對(duì)象時(shí)返回的順序一致(區(qū)別在于 for-in 循環(huán)也枚舉原型鏈中的屬性)
const object1 = { foo: 'bar', baz: '42' };
Object.entries(object1) // [['foo', 'bar'],['baz', '42']]
const object2 = { 0: 'a', 1: 'b', 2: 'c' };
console.log(Object.entries(object2)[2]);
// expected output: Array ["2", "c"]
- Array.every() 方法測(cè)試數(shù)組的所有元素是否都通過了指定函數(shù)的測(cè)試纷宇。
- 符合返回true, 有一項(xiàng)不符合返回false
function isBelowThreshold(currentValue) {
return currentValue < 40;
}
var array1 = [1, 30, 39, 29, 10, 13];
console.log(array1.every(isBelowThreshold));
// expected output: true
-------------
var array1 = [1, 30, 39, 29, 10, 13];
function Maxnumber (Max) { return Max > 50}
array1.every(Maxnumber)
// expected output: false
- insertAdajcentHTML
原型:
insertAdajcentHTML(swhere,stext)
insertAdjacentHTML
方法:在指定的地方插入html標(biāo)簽語句
參數(shù):swhere: 指定插入html標(biāo)簽語句的地方,有四種值可用:
beforeBegin
: 插入到標(biāo)簽開始前
afterBegin
:插入到標(biāo)簽開始標(biāo)記之后
beforeEnd
:插入到標(biāo)簽結(jié)束標(biāo)記前
afterEnd
:插入到標(biāo)簽結(jié)束標(biāo)記后
// 使用方法 示例
append(songs) {
let html = songs.map(song => {
let artist = song.singer.map(s => s.name).join(' ')
return `
<a class="song-item" href="#player?artist=${artist}&songid=${song.songid}&songname=${song.songname}&albummid=${song.albummid}&duration=${song.interval}&songmid=${song.songmid}">
<i class="icon icon-music"></i>
<div class="song-name ellipsis">${song.songname}</div>
<div class="song-artist ellipsis">${artist}</div>
</a>`}).join('')
this.$songs.insertAdjacentHTML('beforeend', html)
}
- abort()
如果該請(qǐng)求已被發(fā)出蛾方,XMLHttpRequest.abort()
方法將終止該請(qǐng)求像捶。當(dāng)一個(gè)請(qǐng)求被終止,它的 readyState 屬性將被置為0( UNSENT )桩砰。
// MDN
var xhr = new XMLHttpRequest(),
method = "GET",
url = "https://developer.mozilla.org/";
xhr.open(method,url,true);
xhr.send();
xhr.abort();
- indexOf()
indexOf()
方法返回在數(shù)組中可以找到一個(gè)給定元素的第一個(gè)索引作岖,如果不存在,則返回-1
var beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];
console.log(beasts.indexOf('bison'));
// expected output: 1
// start from index 2
console.log(beasts.indexOf('bison', 2));
// expected output: 4
console.log(beasts.indexOf('giraffe'));
本人應(yīng)用五芝。表格根據(jù)數(shù)據(jù)返回的尺碼組將其渲染到相對(duì)應(yīng)的尺碼組位置中。
獲取要渲染的尺碼組大小辕万。 通過尺碼組.indexof('尺碼大小')
來確定其位置(不=-1)枢步。將其渲染到相應(yīng)位置即可。
(詳細(xì)示例晚上更新之本人github博客中渐尿,到時(shí)賦值上鏈接)