get-element-by-id-multiple-root-node.uvue 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <template>
  2. <page-head id="page-head" title="getElementById-multiple-root-node"></page-head>
  3. <view class="uni-padding-wrap">
  4. <text id="text">this is text</text>
  5. <view id="view" class="uni-common-mt" style="border: 1px solid red">this is view</view>
  6. <button class="uni-btn" @click="changePageHeadBackgroundColor">
  7. 修改 page-head 背景色
  8. </button>
  9. <button class="uni-btn" @click="changeTextColor">修改 text 字体颜色</button>
  10. <button class="uni-btn" @click="changeViewStyle">
  11. 修改 view 宽高及背景色
  12. </button>
  13. </view>
  14. </template>
  15. <script lang="uts">
  16. export default {
  17. data() {
  18. return {
  19. checked: false,
  20. homePagePath: '/pages/tabBar/component',
  21. launchOptionsPath: '',
  22. }
  23. },
  24. methods: {
  25. getElementByNotExistId() : Element | null {
  26. return uni.getElementById('not-exist-id')
  27. },
  28. changePageHeadBackgroundColor() {
  29. const pageHead = uni.getElementById('page-head')!
  30. pageHead.style.setProperty('background-color', 'red')
  31. },
  32. changeTextColor() {
  33. const text = uni.getElementById('text')!
  34. text.style.setProperty('color', 'red')
  35. },
  36. changeViewStyle() {
  37. const view = uni.getElementById<UniViewElement>('view')
  38. if (view !== null) {
  39. view.style.setProperty('width', '90%')
  40. view.style.setProperty('height', '50px')
  41. view.style.setProperty('background-color', '#007AFF')
  42. }
  43. }
  44. }
  45. }
  46. </script>