Javascript 使用jQuery向iFrame中的DIV容器添加ID

Javascript 使用jQuery向iFrame中的DIV容器添加ID,javascript,html,jquery,iframe,Javascript,Html,Jquery,Iframe,我正在尝试使用jQuery将ID添加到iFrame中的DIV容器中: HTML原件: 如果有人能帮助我,我将非常感激 您的选择器出现故障可能是对的。简化为: $('#iFrame').contents().find('.book-type').each(function(ind) { $(this).attr('id', 'type' + (ind + 1)); }); 您的选择器有故障可能是对的。简化为: $('#iFrame').contents().find('.book-typ

我正在尝试使用jQuery将ID添加到iFrame中的DIV容器中:

HTML原件:


如果有人能帮助我,我将非常感激

您的选择器出现故障可能是对的。简化为:

$('#iFrame').contents().find('.book-type').each(function(ind) {
    $(this).attr('id', 'type' + (ind + 1));
});

您的选择器有故障可能是对的。简化为:

$('#iFrame').contents().find('.book-type').each(function(ind) {
    $(this).attr('id', 'type' + (ind + 1));
});

问题解决了!是否加载iFrame的查询不起作用,因为iFrame不知道ready(),但知道load()

$(document).ready(function() {
    $('#iFrame').load(function() {
        $('#iFrame').contents().find('.book-type').each(function(ind) {
            $(this).attr('id', 'type' + (ind + 1));
        });
    });
});

问题解决了!是否加载iFrame的查询不起作用,因为iFrame不知道ready(),但知道load()

$(document).ready(function() {
    $('#iFrame').load(function() {
        $('#iFrame').contents().find('.book-type').each(function(ind) {
            $(this).attr('id', 'type' + (ind + 1));
        });
    });
});

此外,索引已经是一个整数。不需要解析它。事实上,由于使用了
parseInt()
函数,这可能会出错。没有任何更改,我在JS控制台中也没有收到任何错误。我还用alert()替换了counter函数,但它没有被触发。因此,我怀疑each()的选择器不起作用。而且,索引已经是一个整数了。不需要解析它。事实上,由于使用了
parseInt()
函数,这可能会出错。没有任何更改,我在JS控制台中也没有收到任何错误。我还用alert()替换了counter函数,但它没有被触发。因此,我怀疑each()的选择器不起作用。
$(document).ready(function() {
    $('#iFrame').load(function() {
        $('#iFrame').contents().find('.book-type').each(function(ind) {
            $(this).attr('id', 'type' + (ind + 1));
        });
    });
});