WebSocket股票表格更新时jQuery Flash闪烁效果报错:$row未定义
Hey there, let's break down and fix the issues you're facing step by step:
1. Resolve the "Uncaught ReferenceError: $row is not defined"
The error pops up because you're assigning to $row without declaring it first (using let, const, or var). Plus, child is a raw DOM element—you need to wrap it in a jQuery object ($(child)) to use your custom .flash() method, since that method is added to jQuery's prototype.
Replace this line:
$row = child;
With:
let $row = $(child);
2. Ensure jQuery Can Animate Background Colors
By default, jQuery's .animate() method doesn't support animating color properties like backgroundColor. To get that smooth flash transition working, you'll need to include either:
- The jQuery Color plugin (a lightweight tool focused solely on color animations)
- Or the full jQuery UI library (which includes color animation support alongside other UI features)
Without one of these, the background color will jump abruptly instead of fading in/out.
Corrected Full Code
Here's your updated table update function with all fixes applied:
var child = createStockNode(stock, type, template); // Try to replace existing stock node var stockNode = document.querySelector(type + "[data-symbol=" + stock.n + "]"); if (stockNode) { table.replaceChild(child, stockNode); } else { // Add new stock to table table.appendChild(child); } // Determine flash color based on price change (stock.c is close price) var bg = stock.c < 0 ? '255,148,148' // Red for price drop : '154,240,117'; // Green for price rise // Fix: Declare $row and wrap child in jQuery object let $row = $(child); $row.flash(bg, 1000);
Your existing .flash() method is solid once you fix the above issues, but here it is for reference:
jQuery.fn.flash = function (color, duration) { var current = this.css('backgroundColor'); this.animate({ backgroundColor: 'rgb(' + color + ')' }, duration / 2) .animate({ backgroundColor: current }, duration / 2); };
Quick Final Checks
- Double-check that jQuery is loaded before your custom
.flash()method and table update code - Confirm you've added the jQuery Color or jQuery UI library if you want smooth color transitions
- Make sure
createStockNodereturns a valid DOM element (like a<tr>row) that can have its background color animated
内容的提问来源于stack exchange,提问作者theo




