element-get-attribute.uvue 2.8 KB
Newer Older
Anne_LXM's avatar
Anne_LXM 已提交
1 2 3 4
<template>
  <view>
    <view id="box" ref="box" style="padding: 20rpx;">
      <text class="uni-title-text">元素的id:{{ attrId }}</text>
5
      <!-- #ifndef APP -->
Anne_LXM's avatar
Anne_LXM 已提交
6
      <text class="uni-title-text">元素的style:{{ attrStyle }}</text>
7 8
      <!-- #endif -->
      <text class="uni-title-text">元素的背景色样式值:{{ propertyValue }}</text>
Anne_LXM's avatar
Anne_LXM 已提交
9
      <text class="uni-subtitle-text">小程序端:getAttribute 获取元素的属性值,目前仅支持id、style</text>
10
      <text class="uni-subtitle-text">App端:getAttribute 不支持获取 class、style 属性</text>
Anne_LXM's avatar
Anne_LXM 已提交
11 12 13
    </view>
    <button @click="getAttributeId">getAttribute获取元素的id</button>
    <button @click="setStyle">setProperty设置背景色</button>
14
    <!-- #ifndef APP -->
Anne_LXM's avatar
Anne_LXM 已提交
15
    <button @click="getAttributeStyle">getAttribute获取元素的style</button>
16 17
    <!-- #endif -->
    <button @click="getPropertyValue">getPropertyValue获取背景色值</button>
Anne_LXM's avatar
Anne_LXM 已提交
18 19 20 21 22
    <scroll-view ref="scrollView" class="scroll-view_H" direction="horizontal">
      <view class="scroll-view-item_H uni-bg-red"><text class="text">A</text></view>
      <view class="scroll-view-item_H uni-bg-green"><text class="text">B</text></view>
      <view class="scroll-view-item_H uni-bg-blue"><text class="text">C</text></view>
    </scroll-view>
23
    <!-- #ifndef WEB -->
Anne_LXM's avatar
Anne_LXM 已提交
24
    <button @click="scrollTo">scrollTo设置left滚动200px</button>
25
    <!-- #endif -->
Anne_LXM's avatar
Anne_LXM 已提交
26 27
  </view>
</template>
28
<script lang="uts">
Anne_LXM's avatar
Anne_LXM 已提交
29 30 31 32 33
  export default {
    data() {
      return {
        boxTarget: null as null | UniElement,
        scrollViewTarget: null as null | UniElement,
34 35 36
        attrId: "",
        attrStyle:"",
        propertyValue:""
Anne_LXM's avatar
Anne_LXM 已提交
37 38 39 40 41 42 43 44
      }
    },
    onReady() {
      this.boxTarget = this.$refs['box'] as UniElement
      this.scrollViewTarget = this.$refs['scrollView'] as UniElement;
    },
    methods: {
      scrollTo() {
45
        // #ifdef MP-WEIXIN
Anne_LXM's avatar
Anne_LXM 已提交
46 47 48
        this.scrollViewTarget!.scrollTo({
          left: 200
        })
49 50 51 52
        // #endif
        // #ifndef MP-WEIXIN
        this.scrollViewTarget!.scrollTo(200,0)
        // #endif
Anne_LXM's avatar
Anne_LXM 已提交
53 54
      },
      getAttributeId() {
55
        this.attrId = this.boxTarget!.getAttribute('id') ?? ""
Anne_LXM's avatar
Anne_LXM 已提交
56 57
      },
      setStyle() {
58 59 60 61
        this.boxTarget!.style.setProperty("background-color", "#FFF000")
      },
      getPropertyValue() {
        this.propertyValue = this.boxTarget!.style.getPropertyValue("background-color") as string
Anne_LXM's avatar
Anne_LXM 已提交
62 63
      },
      getAttributeStyle() {
64
        this.attrStyle = this.boxTarget!.getAttribute('style')?? ""
Anne_LXM's avatar
Anne_LXM 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
      }
    }
  }
</script>
<style>
  .scroll-view_H {
    width: 100%;
    flex-direction: row;
    margin-top:30rpx;
  }
  .scroll-view-item_H {
    width: 100%;
    height: 150px;
    justify-content: center;
    align-items: center;
  }
  .text {
    font-size: 18px;
    color: #ffffff;
  }
</style>