[英]Why doesn't jQuery “.css” work with variables?
.css()
works in this case: .css()
在这种情况下有效:
if ($(toggle.targetEl).css('opacity') === '0') {
// some code
} else {
// more code
}
However it doesn't work if you do this: 但是,如果执行此操作,将无法正常工作:
const $targetEl = $(toggle.targetEl)
if ($targetEl.css('opacity') === '0') {
// $targetEl.css('opacity') => undefined
Is this jQuery's default behavior? 这是jQuery的默认行为吗? Or am I doing something wrong? 还是我做错了什么?
EDIT: 编辑:
The last example doesn't output any errors. 最后一个示例不输出任何错误。 And console.log($targetEl)
shows: console.log($targetEl)
显示:
[prevObject: jQuery.fn.init[1], context: document, selector: ".map"]
EDIT 2: 编辑2:
directive.js: 指令.js:
export const toggle = {
update: function (toggle) {
const $el = $(this.el)
const $targetEl = $(toggle.targetEl)
console.log($targetEl)
$el.click(() => {
if ($targetEl.css('opacity') === '0') {
$(toggle.targetEl).css('visibility', 'visible')
$(toggle.targetEl).animate({ opacity: 1 }, 200)
} else {
// hide element when opacity animation is done
$(toggle.targetEl).animate({ opacity: 0 }, 200, () => {
$(toggle.targetEl).css('visibility', 'hidden')
})
}
})
}
}
component.vue: component.vue:
<button class="toggle-map" v-toggle="toggle">
<i class="fa fa-map"></i>
</button>
<script>
export default {
data () {
return {
toggle: {
targetEl: '.map'
}
}
}
Solved the issues in this way: 通过以下方式解决了这些问题:
const $el = $(this.el)
$el.click(() => {
const $targetEl = $(toggle.targetEl)
if ($targetEl.css('opacity') === '0') {
// some code
It's a scope problem with const
... though I'm not very sure. 这是const
的作用域问题……尽管我不太确定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.