简体   繁体   English

jQuery事件更改图像上的嵌入式背景颜色

[英]jquery event on change inline background color on image

I need a help detecting on change event of a background-color inline on "img" tag by a color picker. 我需要一个由颜色选择器检测“ img”标签上的背景色内联的更改事件的帮助。

For example, I have this code: 例如,我有以下代码:

<span class="ui-button-text">
<img id="cp-background" src="transparent.png" style="background-color: rgb(100, 100, 90);"></img>
</span>

I need to detect if style="backgroung-color" as changed with Jquery. 我需要检测是否用Jquery更改了style =“ backgroung-color”。

I have tried this code, but I don't have sucess: 我已经尝试过此代码,但是没有成功:

$("#ok-cp-background").data('bgcolor', $this.css('background-color')).change(function(){
     alert("Changed");
});

Can anyone please help me? 谁能帮帮我吗?

Thank You. 谢谢。

I think the simple solution is using the .trigger() function. 我认为简单的解决方案是使用.trigger()函数。 Your script to attach event listener: 您的脚本来附加事件监听器:

$("#cp-background").on("styleChanged",function(event, propertyName){

});

Extend your $.css() function to call trigger() 扩展$ .css()函数以调用trigger()

(function($)
{
    var oldcss = $.fn.css;
    $.fn.css = function()
    {
        var ret = oldcss.apply(this, arguments);
        if (arguments.length > 1){ //to ensure this function is called to assign a new style.
            this.trigger("styleChanged",arguments);
        }
        return ret;
    };
})(jQuery);

Inside your color picker code, you use .css() to change the background-color. 在颜色选择器代码内部,使用.css()更改背景颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM