gpt4 book ai didi

jquery - $(this) 里面 $ ('#something' ).css({...})

转载 作者:行者123 更新时间:2023-12-01 01:09:43 26 4
gpt4 key购买 nike

我尝试将 css 和 jquery 应用于 div 元素,但它不接受 $(this) 参数。我想知道为什么我不能使用 $(this) 作为 div 元素的引用。

blabla.css({
'top': $(window).width()/2-$(this).width()/2+'px',
'left': $(window).width()/2-$(this).width()/2+'px'
});

错误:未捕获类型错误:对象#没有方法“宽度”

提前致谢..

ps:我知道我可以在 $() 中使用直接 id 调用,但我预计 this 会起作用。

最佳答案

请注意,您没有运行回调,而是将一个对象传递给 .css。因此,this 并不是指 blabla - 元素 - 而是指在调用包含此代码的函数时定义的当前上下文(并且可能会有所不同)。此上下文没有 .width,然后您会收到错误。

您必须显式引用宽度,如 blabla.width()

关于jquery - $(this) 里面 $ ('#something' ).css({...}),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9665028/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com