get-element-by-id-multiple-root-node.uvue 1.5 KB
Newer Older
DCloud-WZF's avatar
DCloud-WZF 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
<template>
  <view class="uni-padding-wrap">
    <page-head id="page-head" title="getElementById-multiple-root-node"></page-head>
    <text id="text">this is text</text>
    <view id="view" class="uni-common-mt" style="border: 1px solid red">this is view</view>
    <button class="uni-btn" @click="changePageHeadBackgroundColor">
      修改 page-head 背景色
    </button>
    <button class="uni-btn" @click="changeTextColor">修改 text 字体颜色</button>
    <button class="uni-btn" @click="changeViewStyle">
      修改 view 宽高及背景色
    </button>
  </view>
</template>

<script lang="uts">
  export default {
    data() {
      return {
        checked: false,
        homePagePath: '/pages/tabBar/component',
        launchOptionsPath: '',
      }
    },
    methods: {
26
      getElementByNotExistId() : Element | null {
DCloud-WZF's avatar
DCloud-WZF 已提交
27 28 29 30
        return uni.getElementById('not-exist-id')
      },
      changePageHeadBackgroundColor() {
        const pageHead = uni.getElementById('page-head')!
雪洛's avatar
雪洛 已提交
31
        pageHead.style.setProperty('background-color', 'red')
DCloud-WZF's avatar
DCloud-WZF 已提交
32 33 34
      },
      changeTextColor() {
        const text = uni.getElementById('text')!
雪洛's avatar
雪洛 已提交
35
        text.style.setProperty('color', 'red')
DCloud-WZF's avatar
DCloud-WZF 已提交
36 37 38 39
      },
      changeViewStyle() {
        const view = uni.getElementById<UniViewElement>('view')
        if (view !== null) {
雪洛's avatar
雪洛 已提交
40 41 42
          view.style.setProperty('width', '90%')
          view.style.setProperty('height', '50px')
          view.style.setProperty('backgroundColor', '#007AFF')
DCloud-WZF's avatar
DCloud-WZF 已提交
43 44 45 46
        }
      }
    }
  }
雪洛's avatar
雪洛 已提交
47
</script>