網(wǎng)站首頁 編程語言 正文
如何動態(tài)更改html標(biāo)簽
比如有這樣的一個需求,子組件通過父組件傳遞過來的props,來動態(tài)顯示<h1>到<h6>標(biāo)簽,可以先不管react,考慮下vue如何實現(xiàn)呢?
Vue的實現(xiàn)方式
父組件
<template>
?? ?<div>
?? ??? ?<Son :tagSize="1"/>
?? ?</div>
</template>
<script>
import Son from '../components/Son';
export default {
? ? name: "Father",
? ? components:{
? ? ? ? Son
? ? }
};
</script>
子組件
<template>
?? ?<div>
? ? ? ? <h1 v-if="tagSize === 1">測試</h1>
? ? ? ? <h2 v-else-if="tagSize === 2">測試</h2>
? ? ? ? <h3 v-else-if="tagSize === 3">測試</h3>
? ? ? ? <h4 v-else-if="tagSize === 4">測試</h4>
? ? ? ? <h5 v-else-if="tagSize === 5">測試</h5>
? ? ? ? <h6 v-else>測試</h6>
?? ?</div>
</template>
<script>
export default {
?? ?name: "Son",
?? ?props: {
?? ??? ?tagSize: {
? ? ? ? ? ? type:Number,
? ? ? ? ? ? default:1
? ? ? ? }
?? ?}
};
</script>
用vue的模板語法我們也是可以實現(xiàn)以上的需求,但是因為不能動態(tài)html標(biāo)簽顯得不是很靈活,可以想象下,如果可以修改標(biāo)簽,直接通過標(biāo)簽拼接的方式
舉例"<h"+tagSize+">測試</h"+tagSize+">"的方式,以這種方式來處理這種需求的話明顯非常方便,廢話不多說,來看看react的實現(xiàn)方式。
React的實現(xiàn)方式
父組件
import React from 'react';
import Child from './Child.js';
class Father extends React.Component{
? ? render(){
? ? ? ? return (
? ? ? ? ? ? <React.Fragment>
? ? ? ? ? ? ? ? <Child size = { 1 }/>
? ? ? ? ? ? </React.Fragment>
? ? ? ? )
? ? }
}
export default Father;
子組件
import React from 'react';
const Child = (props)=>{
? ? let MarkUp = `h${props.size}`;
? ? return (
? ? ? ? <React.Fragment>
? ? ? ? ? ? <MarkUp>你好</MarkUp>
? ? ? ? </React.Fragment>
? ? )
}
export default Child;
相比較兩種方式會發(fā)現(xiàn)react實現(xiàn)這類需求會更合適,react語法采用JSX語法,個人感覺語法比vue的template模板語法更加靈活,不過Vue現(xiàn)在也已經(jīng)支持JSX。
React修改標(biāo)簽頁名
React默認打開的標(biāo)簽頁名為React App,那么我們?nèi)绾胃鶕?jù)我們的項目來全局更改標(biāo)簽頁名呢?
方法
找到項目中的public文件夾,再找到下面的index.html文件,修改其中的
<title>React App</title>
將其中的React App改為自己想要的名字即可
如果要修改圖標(biāo),同樣在該文件中查找相關(guān)的標(biāo)簽進行更改。
原文鏈接:https://blog.csdn.net/weixin_39782183/article/details/104751431
相關(guān)推薦
- 2022-07-18 linux引導(dǎo)和服務(wù)過程
- 2024-01-15 Mybatis 開啟控制臺打印sql語句
- 2022-07-13 python版jpeg合成pdf兩種方法
- 2022-11-02 Android?LeakCanary的使用方法介紹_Android
- 2022-08-20 Python數(shù)據(jù)讀寫之Python讀寫CSV文件_python
- 2022-11-10 react-native?父函數(shù)組件調(diào)用類子組件的方法(實例詳解)_React
- 2022-03-03 關(guān)于使用iview table 組件中使用 tooltip 樣式覆蓋的問題
- 2022-11-28 協(xié)程作用域概念迭代RxTask?實現(xiàn)自主控制_Android
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支