在 html [closed] 中插入后无法显示变量

Variable failed to be shown after inserting in html [closed]

提问人:Jane Lim 提问时间:8/18/2023 最后编辑:Some programmer dudeJane Lim 更新时间:8/18/2023 访问量:38

问:


编辑问题以包括所需的行为、特定问题或错误以及重现问题所需的最短代码。这将帮助其他人回答这个问题。

昨天关闭。

const jQuery = jQuery;
const optNumber = jQuery('.notificationCenter__list li').length;
const html2 = `
<span class="optHeaderMdSm__notification__unread">(jQuery('.notificationCenter__list li').length)</span>
`;
jQuery('.headerLg__notification .link__icon').after(html).remove('.headerLg__notification .link__icon');
JavaScript jQuery 变量

评论

0赞 Some programmer dude 8/18/2023
你的问题是什么?你显示的代码有什么问题?请花一些时间阅读帮助页面,参加 SO 导览,并阅读如何提问。另外,请阅读如何写出“完美”的问题,尤其是它的清单
1赞 Harrison 8/18/2023
如果元素显示的是文本而不是值,请在文本字符串中使用:"jQuery('.notificationCenter__list li').length"${optNumber }const html2 = `<span class="optHeaderMdSm__notification__unread">${optNumber}</span>`;
0赞 Community 8/18/2023
请澄清您的具体问题或提供其他详细信息以准确说明您的需求。正如目前所写的那样,很难确切地说出你在问什么。

答:

-2赞 Sona Rijesh 8/18/2023 #1
const optNumber = jQuery('.notificationCenter__list li').length;
const html2 = `
<span class="optHeaderMdSm__notification__unread">(${optNumber})</span>
`;
jQuery('.headerLg__notification .link__icon').after(html2);

// Remove the link__icon element
jQuery('.headerLg__notification .link__icon').remove();

在使用 jQuery 函数之前,请确保已将 jQuery 库正确包含在 HTML 页面中。