如何将对象的 属性 放入 AJAX 中的 href?

How to put an object's property to a href in AJAX?

我有一个小问题。如何将对象的 属性 放入 href。 我用评论标记了行

success: function (listOfTags) {
        let tagData = '';
        $.each(listOfTags, function (i, tag) {
            // ON THE NEXT LINE
            tagData += '<a href="http://localhost:5557/questions/tagged/" ???tag.id><li class="post-tag">' + tag.name + '</li></a>';
        });

        $('#recentTags').html(tagData);
    }

试试这个:

tagData += `<a href="http://localhost:5557/questions/tagged/${tag.id}"><li class="post-tag">${tag.name}</li></a>`;

对于有效的标记,锚点应该在列表项内,后者应该是 ul 的子项,如下所示:

//before the loop
tagData += '<ul>';

tagData += `<li class="post-tag"><a href="http://localhost:5557/questions/tagged/${tag.id}">${tag.name}</a></li>`;

//after the loop
tagData += '</ul>';