ES6嵌套模板字符串缺失}报错:DOM生成导航栏问题求助
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
mainLinkTextinto 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
letinstead ofvar: It's better practice for block-scoped variables in ES6.
Common Template Literal Pitfalls to Watch For
- Always pair
${with}—even a single missing}will throw that error. - Make sure your template string starts and ends with backticks (
`), not single or double quotes. - When adding HTML attributes, use quotes inside the template string (like
href="${dropdownHref}") to avoid syntax conflicts.
内容的提问来源于stack exchange,提问作者kennsorr

