:if標簽:
完成類似java的if else的功能:
馬克- to-win:馬克 java社區:防盜版實名手機尾號: 73203。
例 2.2.2
<%@ page contentType="text/html; charset=GBK"%>
<!--看看如何完成if else的功能。$就是計算一下的意思。
-->
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page import="java.util.*"%>
<html>
<body>
<c:set var="age" value="13"></c:set>
<c:if test="${age<12}">
你是小學生
</c:if>
<c:if test="${18>age&&age>12}">
你是中學生
</c:if>
<c:if test="${age>18}">
你高于中學生
</c:if>
</body>
</html>
運行jsp后,瀏覽器中輸出結果是:
你是中學生
篇幅有限更多請見擴展鏈接:http://www.mark-to-win.com/tutorial/jsp_5_cifExample.html
家好,html模板的判斷語句和Python里的寫法是一樣的,只不過每一行需要包裹在花括號和百分號里。
·從視圖函數里傳遞一個名稱為user的數據,user變量的值輸入小鐵,在html文件里判斷。如果user返回了數據顯示歡迎你小鐵,否則如果沒有顯示數據顯示請登錄。
·最后用and if來閉合判斷語句,運行Web服務,在瀏覽器里查看效果。因為user這個變量是有數據的,所以前端顯示的歡迎您小鐵。將user變量的值改成無,回到瀏覽器查看效果,這個時候瀏覽器顯示的是請登錄。
這就是html模板文件里if判斷語句的使用方法。
于parseHTML函數代碼實在過于龐大,我這里就不一次性貼出源代碼了,大家可以前往(https://github.com/vuejs/vue/blob/dev/src/compiler/parser/html-parser.js)查看源代碼。
我們來總結一下該函數的主要功能:
1、匹配標簽的 "<" 字符
匹配的標簽名稱不能是:script、style、textarea
有如下情況:
1、注釋標簽 /^<!\--/
2、條件注釋 /^<!\[/
3、html文檔頭部 /^<!DOCTYPE [^>]+>/i
4、標簽結束 /^<\/ 開頭
5、標簽開始 /^</ 開頭
然后開始匹配標簽的屬性包括w3的標準屬性(id、class)或者自定義的任何屬性,以及vue的指令(v-、:、@)等,直到匹配到 "/>" 標簽的結尾。然后把已匹配的從字符串中刪除,一直 while 循環匹配。
解析開始標簽函數代碼:
function parseStartTag () {
// 標簽的開始 如<div
const start=html.match(startTagOpen)
if (start) {
const match={
tagName: start[1], // 標簽名稱
attrs: [], // 標簽屬性
start: index // 開始位置
}
// 減去已匹配的長度
advance(start[0].length)
let end, attr
while (!(end=html.match(startTagClose)) && (attr=html.match(dynamicArgAttribute) || html.match(attribute))) {
attr.start=index
v
advance(attr[0].length)
attr.end=index
match.attrs.push(attr) // 把匹配到的屬性添加到attrs數組
}
if (end) { // 標簽的結束符 ">"
match.unarySlash=end[1]
advance(end[0].length) // 減去已匹配的長度
match.end=index // 結束位置
return match
}
}
}
處理過后結構如下:
接下來就是處理組合屬性,調用 “handleStartTag” 函數
function handleStartTag (match) {
const tagName=match.tagName // 標簽名稱
const unarySlash=match.unarySlash // 一元標簽
if (expectHTML) {
if (lastTag==='p' && isNonPhrasingTag(tagName)) {
// 解析標簽結束
parseEndTag(lastTag)
}
if (canBeLeftOpenTag(tagName) && lastTag===tagName) {
parseEndTag(tagName)
}
}
// 是否為一元標簽
const unary=isUnaryTag(tagName) || !!unarySlash
const l=match.attrs.length
// 標簽屬性集合
const attrs=new Array(l)
for (let i=0; i < l; i++) {
const args=match.attrs[i]
const value=args[3] || args[4] || args[5] || ''
const shouldDecodeNewlines=tagName==='a' && args[1]==='href' ? options.shouldDecodeNewlinesForHref : options.shouldDecodeNewlines
attrs[i]={
name: args[1], // 屬性名稱
value: decodeAttr(value, shouldDecodeNewlines) // 屬性值
}
if (process.env.NODE_ENV !=='production' && options.outputSourceRange) {
// 開始位置
attrs[i].start=args.start + args[0].match(/^\s*/).length
// 結束位置
attrs[i].end=args.end
}
}
if (!unary) {
stack.push({ tag: tagName, lowerCasedTag: tagName.toLowerCase(), attrs: attrs, start: match.start, end: match.end })
lastTag=tagName
}
// 調用start函數
if (options.start) {
options.start(tagName, attrs, unary, match.start, match.end)
}
}
我們簡單說一下最后調用的start函數的作用:
1、判斷是否為svg標簽,并處理svg在ie下的兼容性問題
2、遍歷標簽屬性,驗證其名稱是否有效
3、標簽名是否為 style 或者 script ,如果在服務端會提示warn警告
4、檢查屬性是否存在 v-for、v-if、v-once指令
5、如果是更元素就驗證其合法性,不能是 slot 和 template 標簽,不能存在 v-for指令
以上就是界面html模板的開始標簽的分析,接下來我們來分析如何匹配結束標簽。
請看:Vue源碼全面解析三十 parseHTML函數(解析html(二)結束標簽)
如有錯誤,歡迎指正,謝謝。
*請認真填寫需求信息,我們會在24小時內與您取得聯系。