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

Include additional content in append code jquery

简单的问题(我认为)

这个...

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

...显示所有 div 中包含的带有 class .jg 的图像文件的名称。它将名称包装在 h6 标签中并将其自身附加到其关联的 .jg div。

^^^ 没问题^^^

我的问题。

如何向 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>'
        }));
    });
});

Demo

你可以试试这个代码

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