[英]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.