You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ES6嵌套模板字符串缺失}报错:DOM生成导航栏问题求助

Fixing the "Missing } in template expression" Error in ES6 Template Strings

Hey there! Let's work through that syntax error you're hitting with your navigation bar code. As someone new to ES6 template literals, it's totally normal to stumble on these small but tricky details—let's break this down.

First, that error message Uncaught SyntaxError: Missing } in template expression tells us one of two things is going wrong:

  • You've got an incomplete ${} expression (forgot to close it with })
  • Your template string itself is incomplete (missing closing backticks, or unclosed HTML tags)

Looking at your code snippet, the first red flag is that it cuts off mid-HTML: <ul class="nav-dropdown"> <li><a href=...—this unfinished HTML and unclosed template string is definitely causing part of the problem. Let's fix this step by step.

Step 1: Complete Your Template String & HTML Tags

First, make sure every opening HTML tag has a closing tag, and your template string is wrapped properly with backticks. For example, if you're building dropdown items, you need to close the <li>, <ul>, and finish the template string segment.

Step 2: Simplify Dynamic Content to Avoid Expression Mistakes

Instead of cramming long jQuery calls directly into ${} blocks, extract dynamic values into variables first. This makes it easier to spot missing brackets or syntax issues.

Corrected Code Example

Here's a cleaned-up version of your function that fixes the syntax error and makes the code more readable:

function makeNavList() {
  let myList = '';
  
  $("#superfish-1>li").each(function (index, listItem) {
    // Extract main link text first
    const mainLinkText = $(listItem).find(">a").text();
    
    // Build dropdown items by mapping over child list items
    const dropdownItems = $(listItem).find(">ul li").map(function() {
      const dropdownHref = $(this).find(">a").attr('href');
      const dropdownText = $(this).find(">a").text();
      return `<li><a href="${dropdownHref}">${dropdownText}</a></li>`;
    }).get().join('');
    
    // Add the full list item to our nav string
    myList += `
      <li>
        <a href="#!">${mainLinkText}</a>
        <ul class="nav-dropdown">
          ${dropdownItems}
        </ul>
      </li>
    `;
  });
  
  // Don't forget to insert the generated HTML into your page!
  // Example: $('#your-nav-container').html(myList);
}

Key Fixes & Tips

  • No more incomplete templates: Every HTML tag is closed, and the template string is properly wrapped with backticks.
  • Extracted variables: By pulling dynamic values like mainLinkText into separate variables, we avoid messy nested code inside ${} blocks that's easy to mess up.
  • Using .map() for dropdowns: This is a cleaner way to generate multiple dropdown items instead of building the string manually in a loop, reducing the chance of syntax errors.
  • ES6 let instead of var: It's better practice for block-scoped variables in ES6.

Common Template Literal Pitfalls to Watch For

  1. Always pair ${ with }—even a single missing } will throw that error.
  2. Make sure your template string starts and ends with backticks (`), not single or double quotes.
  3. When adding HTML attributes, use quotes inside the template string (like href="${dropdownHref}") to avoid syntax conflicts.

内容的提问来源于stack exchange,提问作者kennsorr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:12:41