简体   繁体   中英

Javascript to jQuery translation

I have just started studying jquery and javascript and have encountered a line of code that I dont know how to translate. Any help would be much appreciated. Id like to translate it from JavaScript to jQuery so that I can use classes. Here are the lines of code.

    var rgb = getAverageRGB(document.getElementById('try'));
document.body.style.backgroundColor = 'rgb('+rgb.r+','+rgb.g+','+rgb.b+')';

You're mixing things up with that second line:

$('.post').css("background-color", 'rgb('+rgb.r+','+rgb.g+','+rgb.b+')');

And the jQuery way to find an element by its "id" value is

var rgb = getAverageRGB($('#try')[0]);

The $('#try') part creates a jQuery-wrapped list of nodes that match the selector, so in this case it'll be just one node. However, presuming that API expects a DOM node and not a jQuery wrapper, the trailing [0] extracts the raw DOM node from the jQuery wrapper.

Keep in mind that jQuery is JavaScript — we're not talking about two different languages.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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