在附加代码jquery中包含附加内容

Include additional content in append code jquery

本文关键字:包含附 jquery 代码      更新时间:2023-09-26

简单的问题(我认为)

jQuery(function($) {
    $(".jg").each(function() {
        $(this).append($('<h6/>', {
            text: $(this).find('a').attr('href').split('/').pop()
        }));
    });
});

…显示包含在类. jj的所有div中的图像文件的名称。它将名称包装在h6标签中,并将自己附加到与之关联的.jgdiv中。

^^^没有问题^^^

我的问题。

我如何添加额外的文本到H6?

我想要这个…

text: $(this).find('a').attr('href').split('/').pop()

加上这个……

("Tada!");

除了H6之外,我还可以将它附加到。jg中,如…

jQuery(function($) {
    $(".jg").each(function() {
        $(this).append($('<h6/>', {
            text: $(this).find('a').attr('href').split('/').pop()
        }));
        $(this).append("Tada!");
    });
});

但我只是想要它包含在H6和不知道语法吗?

如…

jQuery(function ($) {
    $(".jg").each(function () {
        $(this).append($('<h6/>', {
            text: $(this).find('a').attr('href').split('/').pop()
            **PLUS SOMETHING ELSE HERE**
        }));
    });
});

我确信这是基本的jQuery语法,不幸的是,无论我尝试打破它:(

谢谢!

将文本连接到each循环中.jgtext

text: $(this).find('a').attr('href').split('/').pop() + 'Tada!'
//                                                    ^^^^^^^^^
代码:

jQuery(function($) {
    $(".jg").each(function() {
        $(this).append($('<h6/>', {
            html: $(this).find('a').attr('href').split('/').pop() + '<span>Tada!</span>'
        }));
    });
});

您可以试试下面的代码

jQuery(function($) {
    $(".jg").each(function() {
        $(this).append('<h6>' + $(this).find('a').attr('href').split('/').pop() + 'Tada!</h6>');
    });
});