6月份的時候做過一段時間的vue開發,之後換工作,了解到目前所在的公司是打算使用node來做服務端渲染,當時對node的接觸很少,或許可以通過這個項目,可以讓我對node有更多的了解,是以就決定接受目前這家公司的offer。在開發過程中遇到了很多問題,在這裡做記錄備忘。
express + jade + layui
express通過post請求跳轉頁面
在項目首頁,點設定按鈕會調轉到使用者的設定中心頁面,剛開始是直接通過更改location.href的路徑并且将一些參數拼接到url後面的方式來做頁面跳轉,但是這樣貌似看着不是很好看,是以就決定将跳轉的方式改為post的方式,将需要渲染的資料通過post的方式傳給express路由,下面上一個小例子:
$.ajax({
url:userPath+'/menu',
type:'POST',
async:false,
data:{
menuArr:JSON.stringify(menuArr)
},
success:function(res){
if(res === 'OK'){
location.href = userPath+'/userinfo-company'
}
},
error:function(error){
console.log(error);
}
})
router.post('/menu', function(req, res, next) {
menuArr = JSON.parse(req.body.menuArr);
res.send('OK');
});
router.get('/userinfo-company', function(req, res, next) {
subMenuArr = menuArr[0];
res.render(
'userInfo/company',
{
title: '企業資料',
menuArr: menuArr,
subMenuArr:subMenuArr
}
);
});
menu需要從背景請求,通過jade來渲染,是以需要将menu的數組通過post的方式來傳遞給index路由。
用js給select标簽的option選項設定選中狀态
做三級關聯的時候,需要給的select動态加載option選項,并且需要動态設定預設狀态,我當時首先想到的就是
$(".selector").find("option[text='xxx']").attr("selected",true);
但是經過測試這種方法沒有起作用,因為[]是attribute選擇器,text是property,是以選不到,這裡可以用JQuery的
$(".selector").find("option:contains('pxx')").attr("selected",true);
選擇器給option設定預設選中狀态。
attribute和property的不同
<p>property是DOM中的屬性,是JavaScript裡的對象,每一個dom節點都是一個對象,這些dom對象有很多屬性,例如:clientHeight,className,innerHTML,innerText等;</p>
<p>attribute是HTML标簽上的特性(即:标簽所特有的屬性),它的值隻能夠是字元串,例如:class,id,value等;</p>
下面一行代碼看懂attribute和property:
<!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div class="box" id='obox' title='divTitle' name='divName'></div>
<script type="text/javascript">
var obox = document.getElementById('obox');
console.dir(obox);
</script>
</body>
</html>
列印的輸出結果如下,同時我們還可以在第四行看到attribute屬性