[英]After AJAX call, newly loaded CSS selectors not available to jQuery.each()
NOTE Though I'll defer to the wisdom of the herd, I don't agree that this is a duplicate (at least not as linked), and I was unable to find the answer to my problem on the linked page. 注意尽管我会顺其自然,但我不同意这是重复的(至少不是链接的),并且无法在链接的页面上找到问题的答案。
Goal: 目标:
Dilemma: 困境:
After finishing (1) above, the newly loaded selectors don't seem to be available. 完成上述(1)之后,新加载的选择器似乎不可用。 I've been trying variations of
jQuery.on()
to try to get jQuery to register these newly added DOM elements. 我一直在尝试
jQuery.on()
变体,以尝试使jQuery注册这些新添加的DOM元素。 But I don't need event handling; 但是我不需要事件处理。 I want to immediately alter the CSS of some of the newly arrived elements.
我想立即更改一些新到达元素的CSS。 I've tried a dozen variations on the below—most of which attempt to make the style changes within the
AJAX.success()
callback, rather than after it; 我在下面尝试了十几种变体,其中大多数尝试在
AJAX.success()
回调中而不是在回调之后进行样式更改。 this was just my last effort. 这只是我最后的努力。
This has to be something simple, but I'm an hour in and I jsut can't see it... 这必须是简单的东西,但我在一个小时内,我仅仅指刚还看不出来?
Here's a sample of the loaded HTML: 这是加载的HTML的示例:
<div class="jsonprinter-row indented-0 odd-row has-children" data-tab-offset="0" data-key-offset="0">
<div class="jsonprinter-key">category</div>
<div class="jsonprinter-list">
<div class="jsonprinter-row indented-1 even-row" data-tab-offset="1" data-key-offset="10">
<div class="jsonprinter-key">key</div>
<div class="jsonprinter-value">val</div>
</div>
</div>
</div>
Aaaaand my JS: Aaaa和我的JS:
var tab_size = 8;
var monospace_char = 3;
function updatePrintedRows(element) {
var data = $(element).data();
var width = data['tab-offset'] * tab_size + data['key-offset'] * monospace_char;
$(element).css({"padding-left": toString(width)+"px"});
}
// note that I've tried both:
$(".jsonprinter-row").on("load", function() {
updatePrintedRows(this);
});
// and also:
$(document).on("load", ".jsonprinter-row", function() {
updatePrintedRows(this);
});
$("#printsheet").on('click', function() {
$("#sheet-view").html( function() {
var sheetData = // points to a Js object
$.get("sheet_string.php", {sheet:sheetData}, function(data) {
$("#sheet-view-content").html(data);
});
});
});
If event delegation doesn't work for the load
event, you need to call the function in the callback: 如果事件委托不适用于
load
事件,则需要在回调中调用该函数:
$("#printsheet").on('click', function() {
$("#sheet-view").html( function() {
var sheetData = // points to a Js object
$.get("sheet_string.php", {sheet:sheetData}, function(data) {
$("#sheet-view-content").html(data);
$("#sheet-view-content .jsonprinter-row").each(function() {
updatePrintedRows(this);
});
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.