【问题标题】:Vue-multiselect - How to insert html code in placeholder?Vue-multiselect - 如何在占位符中插入 html 代码?
【发布时间】:2019-10-02 12:14:32
【问题描述】:

我正在尝试在占位符中插入跨度,以更改颜色。但是占位符只返回字符串,如何解决?

computed: {
      customPlaceholder () {
        let numLength = this.options.length;
        return this.placeholder + "<span>"+numLength+"</span>"
      }
}

【问题讨论】:

  • 你能用模板显示所有代码吗?

标签: vue.js vue-multiselect


【解决方案1】:

我认为您正在尝试在 input 字段中添加自定义占位符。

要做到这一点,您需要混合使用 csshtml

new Vue({
  el: '#editor',
  data: {
    input: '',
    input2: 'some text'
  },
  computed: {
    placeholderText: function () {
      return `${this.input2} <span>*</span>`
    }
  },
  methods: {
    update: _.debounce(function (e) {
      this.input = e.target.value
    }, 300)
  }
})
#editor div {
  display: inline-block;
}



.input-placeholder {
  position: relative;
}
.input-placeholder input {
  padding: 10px;
  font-size: 25px;
}
.input-placeholder input:valid + .placeholder {
  display: none;
}

.placeholder {
  position: absolute;
  pointer-events: none;
  top: 0;
  bottom: 0;
  height: 25px;
  font-size: 25px;
  left: 10px;
  margin: auto;
  color: #ccc;
}

.placeholder span {
  color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://unpkg.com/lodash@4.16.0"></script>

<div id="editor">
  <div class="input-placeholder">
    <input type="text" @input="update">
    <div class="placeholder" v-if="!input" v-html="placeholderText">
      Email <span>*</span>
    </div>
  </div>
</div>

我为我的解决方案创建了这个jsfiddle

【讨论】:

  • 你太棒了,谢谢你
【解决方案2】:

你可以使用css占位符选择器

input::-webkit-input-placeholder { /* Edge */
  color: green!important;
}

input:-ms-input-placeholder { /* Internet Explorer 10-11 */
  color: green!important;
}

input::placeholder {
  color: green!important;
}

试试看,然后尝试删除!important

但是缺少信息。你想动态改变颜色吗?或者您想在同一个占位符中使用不同的颜色?

【讨论】:

  • 不,我需要插入 html,而不是颜色,并且在 vue 中不输入
  • 你应该放更多的代码。组件代码或其他内容,以便我们猜测您在说什么。你的意思是这不是一个输入......什么是?一个div?跨度?试试v-html 例如&lt;span v-html="customPlaceholder"&gt;&lt;/span&gt;
  • {{ placeholder }}
猜你喜欢
  • 1970-01-01
  • 2019-12-29
  • 2016-07-11
  • 1970-01-01
  • 2016-06-24
  • 2011-10-25
  • 2013-11-01
  • 2015-10-21
  • 1970-01-01
相关资源
最近更新 更多