javascript - JavaScript 中的字符串插值

标签 javascript jquery

在 Ruby 中,您可以像这样使用字符串插值:

text = "This is visit number #{numVisits} to this website"

这绕过了显式连接的需要。

我正在使用 jQuery,有点像这样:

$(document).ready(function(){
    $("a.ajax").click(function(event){
        $("#content").load("data.html this.getClass");
    });
});

我想要的行为是“点击<a class="ajax" id="schedule"></a>,当前页面上的content divschedule div替换为data.html。如果我手动写入

load("data.html #test"); 

这行得通,但我希望脚本使用单击的 anchor 的 ID 值加载 DIV。任何帮助都会膨胀!

示例页面:http://www.mariahandalex.info/stack/

最佳答案

这已经改变了。

截至 2015 年,现在有更好的方法: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/template_strings

将 Ruby 和 PHP 等许多语言已经享有的东西带到 JS 中。

例如,在我使用 jQuery 驱动的页面中:

   $('#myModalLabel').html(`Show GRAPH of : ${fundCode}-${funcCode}`); 

在我更新的 Firefox 和 Chrome 中安全地呈现为:

Show GRAPH of : AIP001-_sma

请注意在 .html(....) 中围绕字符串参数使用反引号

关于javascript - JavaScript 中的字符串插值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/845952/

相关文章:

javascript - componentDidUpdate 创建无限循环并且不确定如何避免它

JQuery按名称选择问题

javascript - 使用Javascript计算固定高度导航下包装器div的高度

jquery - 对话框定位

javascript - ES6 模块未按预期在 React 中导入

javascript - SetTimeout 重叠?

javascript - 使用 id 作为 var 来访问 DOM 元素

javascript - 当 WebKit WebApp 为 "terminated"时会触发什么事件

jquery - Bootstrap Datepicker 来自属性的开始日期

jquery - 全日历删除标题仍然显示小单元格