切换背景图像 (jQuery)

发布于 2024-09-06 06:45:19 字数 288 浏览 2 评论 0原文

$('textarea').focus(function() {
    var img = $(this).css('background-image');
    $(this).css('background-image', 'none');
});
$('textarea').blur(function() {
    $(this).css('background-image', img);
});

..似乎不起作用。我觉得有些不对劲,但又想不出来到底是什么。

非常感谢您的帮助!

$('textarea').focus(function() {
    var img = $(this).css('background-image');
    $(this).css('background-image', 'none');
});
$('textarea').blur(function() {
    $(this).css('background-image', img);
});

.. doesn't seem to work. I think somethings wrong, but I can't figure out what.

Many thanks for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅浅 2024-09-13 06:45:19

如果您仅在 .focus() 事件处理程序中定义

var img

,则该变量将在以下范围内不可用
.blur()

因此,要么全局定义 var img,要么使用 jQuery 的 .data() 方法。

写:

$.data(this, 'img', $(this).css('background-image'));

读:

$.data(this, 'img');

示例:

$('textarea').focus(function() {
   var $this = $(this);
   $.data(this, 'img', $this.css('background-image'));
   $this.css('background-image', 'none');
});
$('textarea').blur(function() {
   $(this).css('background-image', $.data(this, 'img') || '');
});

If you define

var img

just inside the .focus() event handler, that variable will not be available within
.blur()

So either define var img globaly, or use jQuerys .data() method for instance.

write:

$.data(this, 'img', $(this).css('background-image'));

read:

$.data(this, 'img');

example:

$('textarea').focus(function() {
   var $this = $(this);
   $.data(this, 'img', $this.css('background-image'));
   $this.css('background-image', 'none');
});
$('textarea').blur(function() {
   $(this).css('background-image', $.data(this, 'img') || '');
});
一笔一画续写前缘 2024-09-13 06:45:19

尝试在 css 中定义图像的 url 地址。

var img = 'images/mybg.png';
$('textarea').blur(function() {
    $(this).css('background-image', 'url('+img+')');
});

Try to define the url-address for the image in css.

var img = 'images/mybg.png';
$('textarea').blur(function() {
    $(this).css('background-image', 'url('+img+')');
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文