我有以下jquery代码将#contentdiv从另一个页面加载到当前页面的#resultdiv中:
$('a').click(function(event){
$('#result').load('abother_page.html #content');
});
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我将所请求文件的名称以及我想要显示的特定div硬编码到使用load方法发送的字符串中.
我试图让这个动态,但我的代码遗漏了一些东西:
// get the href of the link that was clicked
var href=$(this).attr('href');
// pass the href with the load method
$('#result').load('href #content');
Run Code Online (Sandbox Code Playgroud)
显然这不起作用,因为我创建的href变量没有插入字符串中.我怎样才能做到这一点?
我尝试了以下,但都没有成功:
// Ruby-style:
$('#result').load('#{href} #content');
// Concatenating-style
$('#result').load(href + '#content');
Run Code Online (Sandbox Code Playgroud)
Jos*_*eph 12
添加"here"指示的空格:
$('#result').load(href + ' #content');
^---- here
Run Code Online (Sandbox Code Playgroud)
失败尝试的解释如下:
//this code used the string "href" as the url
$('#result').load('href #content');
//javascript doesn't have this syntax for wrapping variables like in PHP
$('#result').load('#{href} #content');
//this code appended "#content" like it was a hash tag
$('#result').load(href + '#content');
Run Code Online (Sandbox Code Playgroud)