在 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
div
被schedule
div
替换为data.html
。如果我手动写入
load("data.html #test");
这行得通,但我希望脚本使用单击的 anchor 的 ID 值加载 DIV。任何帮助都会膨胀!
最佳答案
这已经改变了。
截至 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/