[英]String interpolation in JavaScript
在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
将被data.html
的schedule
div
data.html
。如果我手动写入
load("data.html #test");
这工作,但我希望脚本加载DIV与点击锚点的ID值。 任何帮助都会膨胀!
示例页面: http : //www.mariahandalex.info/stack/
这已经改变了。
截至2015年,现在有一种更好的方法: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/template_strings
为JS带来了许多像Ruby和PHP这样的语言已经享受过的东西。
例如,在我的jQuery驱动页面中,我使用:
$('#myModalLabel').html(`Show GRAPH of : ${fundCode}-${funcCode}`);
在我更新的Firefox和Chrome中安全地呈现为:
显示GRAPH:AIP001-_sma
注意在.html( .... )中使用围绕字符串参数的反引号
Javascript不解析字符串文字,寻找像Ruby或PHP那样的替换标记。 您将不得不使用串联。
在es6中这是可能的
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/template_strings
`string text ${expression} string text`
试试这个:
$(document).ready(function(){
$("a.ajax").each(function(){
var obj = $(this);
obj.click(function(event){
alert(obj.attr('id'));
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.