ES6-字符串模板

//原始字符串拼接寫法
const name = 'Asher';
const age = 5;
const sentence = name + ' is ' + age + ' years old';
console.log(sentence);    //Asher is 5 years old
//ES6中拼接字符串
//``定義字符串
//{}中可以是任何JS表達(dá)式拍屑,變量左医、函數(shù)痹雅、對象的屬性等
const name = 'Asher';
const age = 5;
const sentence = `${name} is ${age * 2} years old`;
console.log(sentence);    //Asher is 10 years old
//ES6的模板字符串
const name = 'Asher';
const template = `<div>${name}</div>`;
console.log(template);          //<div>Asher</div>
//最常用的就是在vue中寫一個模板的時候丙挽,方便多了
const template = `
    <div class="box">
        <p>hello</>
    </div>
`;
  • 模板字符串使用案例一
1.模板字符串可嵌套使用
2.模板字符串內(nèi)可以使用三元表達(dá)式
<style type="text/css">
    .todoList {
        width: 300px;
        border :1px solid #B0E2FF;
        border-radius: 3px;
    }
    .userName {
        padding: 10px 0 10px 10px;
        background: #B0E2FF;
    }
    .date {
        padding: 5px 0 5px 10px;
        background: #B0E2FF;
    }
</style>
<script type="text/javascript">
const Asher = {
    name: 'Asher',
    date: '2017-05-30',
    todos: [
        {
            item: 'go to store',
            completed: false
        },
        {
            item: 'go to store',
            completed: true
        },
        {
            item: 'go to store',
            completed: true
        }
    ]
};
function renderTodos(todos){
    return (
        `
        <ul>
            ${todos.map( todo => `
                <li>
                    ${todo.item} ${todo.completed ? '√' : '×'}
                </li>` ).join('')}
        </ul>
        `
    )
}
const template = `
    <div class="todoList">
        <div class="userName">${Asher.name}</div>
        <div class="list">
        ${renderTodos(Asher.todos)}
        </div>
        <div class="date">${Asher.date}</div>
    </div>
    
`;
document.body.innerHTML = template;
</script>
string template-1.PNG
  • 標(biāo)簽?zāi)0遄址?/li>
//使用模板字符串掖举,sentence返回"Asher has commented on your topic Learn to use es6"
function highLight (){
    return 'es6';
}
const user = 'Asher';
const topic = 'Learn to use es6';
const sentence = `${user} has commented on your topic ${topic}`
//使用標(biāo)簽?zāi)0遄址笥蚊ぃ祷?es6"介袜,僅返回標(biāo)簽函數(shù)所返回的內(nèi)容
function highLight (){
    return 'es6';
}
const user = 'Asher';
const topic = 'Learn to use es6';
const sentence = highLight`${user} has commented on your topic ${topic}`
//標(biāo)簽函數(shù)仪芒,...value表示剩余參數(shù)唁影,參數(shù)結(jié)解析如下圖
//strings里有2個空字符串的原因是模板字符串以變量開頭結(jié)尾
function highLight (strings, ...values){
    debugger;
}
const user = 'Asher';
const topic = 'Learn to use es6';
const sentence = highLight`${user} has commented on your topic ${topic}`
tagged template arguments.PNG
  • 標(biāo)簽?zāi)0遄址咐?/li>
//效果如下,
<style type="text/css">
    .highLight {
        padding: 2px 5px;
        background: #B0E2FF;
    }
</style>
<script type="text/javascript">
function highLight (strings, ...values){
    const highLighted = values.map(value => `<span class="highLight">${value}</span>`)

    let str = '';

    strings.forEach((string,i) => str += `${string}${highLighted[i] || ``}`)

    return str;
}

const user = 'Asher';
const topic = 'Learn to use es6';
const sentence = highLight`${user} has commented on your topic ${topic}`

document.body.innerHTML = sentence;
</script>
tagged template example.PNG
  • 標(biāo)簽函數(shù)的用途掂名,可以用來過濾用戶輸入
<style type="text/css">
    .comment-text {
        padding: 10px;
        width: 350px;
        height: 150px;
        border: 1px solid #B0E2FF;
        border-radius: 3px;
    }
    button {
        display: block;
        margin: 10px 0 20px;
        padding: 10px 15px;
        background: #B0E2FF;
        border: 1px solid #B0E2FF;
        border-radius: 3px;
        color: #333;
        cursor: pointer;
    }
    .userName {
        padding-bottom: 15px;
        border-bottom: 1px solid #ccc;
        color: #F08080;
        font-size: 18px;
    }
    .comentCotent {
        padding: 15px 0;
    }
</style>
<body>
    <div class="box">
        <form class="add-comment">
            <textarea class="comment-text"></textarea>
            <button>Post Comment</button>
        </form>
        <div class="comment"></div>
    </div>

    <script type="text/javascript">
        const addCommentForm = document.querySelector(".add-comment");
        const textarea = document.querySelector(".comment-text");
        const comment = document.querySelector(".comment");
        const user = 'Asher';

        function filter(strings,...values){
            //這里可以寫上過濾需求
        }
        addCommentForm.addEventListener("submit",function(evt){
            evt.preventDefault();
            const newComment = textarea.value.trim();
            if(newComment) {
                comment.innerHTML = filter`
                    <div class="userName">${user}</div>
                    <div class="comentCotent">${textarea.value}</div>
                `;
                textarea.value = '';
            }
        })
    </script>
</body>
filter.PNG
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末据沈,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子饺蔑,更是在濱河造成了極大的恐慌锌介,老刑警劉巖,帶你破解...
    沈念sama閱讀 211,042評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件膀钠,死亡現(xiàn)場離奇詭異掏湾,居然都是意外死亡裹虫,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 89,996評論 2 384
  • 文/潘曉璐 我一進店門融击,熙熙樓的掌柜王于貴愁眉苦臉地迎上來筑公,“玉大人,你說我怎么就攤上這事尊浪∠宦牛” “怎么了?”我有些...
    開封第一講書人閱讀 156,674評論 0 345
  • 文/不壞的土叔 我叫張陵拇涤,是天一觀的道長捣作。 經(jīng)常有香客問我,道長鹅士,這世上最難降的妖魔是什么券躁? 我笑而不...
    開封第一講書人閱讀 56,340評論 1 283
  • 正文 為了忘掉前任,我火速辦了婚禮掉盅,結(jié)果婚禮上也拜,老公的妹妹穿的比我還像新娘。我一直安慰自己趾痘,他們只是感情好慢哈,可當(dāng)我...
    茶點故事閱讀 65,404評論 5 384
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著永票,像睡著了一般卵贱。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上侣集,一...
    開封第一講書人閱讀 49,749評論 1 289
  • 那天键俱,我揣著相機與錄音,去河邊找鬼肚吏。 笑死方妖,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的罚攀。 我是一名探鬼主播,決...
    沈念sama閱讀 38,902評論 3 405
  • 文/蒼蘭香墨 我猛地睜開眼雌澄,長吁一口氣:“原來是場噩夢啊……” “哼斋泄!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起镐牺,我...
    開封第一講書人閱讀 37,662評論 0 266
  • 序言:老撾萬榮一對情侶失蹤炫掐,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后睬涧,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體募胃,經(jīng)...
    沈念sama閱讀 44,110評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡旗唁,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,451評論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了痹束。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片检疫。...
    茶點故事閱讀 38,577評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖祷嘶,靈堂內(nèi)的尸體忽然破棺而出屎媳,到底是詐尸還是另有隱情,我是刑警寧澤论巍,帶...
    沈念sama閱讀 34,258評論 4 328
  • 正文 年R本政府宣布烛谊,位于F島的核電站,受9級特大地震影響嘉汰,放射性物質(zhì)發(fā)生泄漏丹禀。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 39,848評論 3 312
  • 文/蒙蒙 一鞋怀、第九天 我趴在偏房一處隱蔽的房頂上張望双泪。 院中可真熱鬧,春花似錦接箫、人聲如沸攒读。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,726評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽薄扁。三九已至,卻和暖如春废累,著一層夾襖步出監(jiān)牢的瞬間邓梅,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,952評論 1 264
  • 我被黑心中介騙來泰國打工邑滨, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留日缨,地道東北人。 一個月前我還...
    沈念sama閱讀 46,271評論 2 360
  • 正文 我出身青樓掖看,卻偏偏與公主長得像匣距,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子哎壳,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 43,452評論 2 348

推薦閱讀更多精彩內(nèi)容