在 JS 字串中拼接 HTML 事件屬性時,常因引號層級過多導致語法錯誤:
// 錯誤:內層雙引號與外層衝突
html += '<a onclick="return removeOpenCss(' + e.point.lng + e.point.lat + ')">取消</a>';
// 錯誤:嘗試加雙引號卻導致字串中斷
html += '<a onclick="return removeOpenCss("' + e.point.lng + e.point.lat + '")">取消</a>';
**原則:在 JS 字串內部,用 HTML 實體編碼表示引號。**
' → ' 或 '(HTML5)" → "html += '<a onclick="return removeOpenCss("' + e.point.lng + e.point.lat + '")">取消</a>';
瀏覽器解析 HTML 時," 會還原為 ",JS 執行時看到的是合法的 removeOpenCss("123.45")。
<input type="button" value="Click Me"
onclick="showMsg('"FSDFDS"')" />
<input type="text" id="txtName" value="'"FSDFDS" />
<script>
function showMsg(s) { alert(s); } // 彈出: "FSDFDS"
</script>
addEventListener / jQuery .on() 綁定,完全跳脫引號地獄。`...`)搭配 ${var},可直接包含雙引號:const html = `取消`;