我试图使用ajax加载某些页面,但'点击'不工作。我创建了一个init()函数,以便其中一个页面在加载时自动加载。那工作!然而,当我点击任何链接时,什么都没有加载。事实上,什么也没发生。一旦链接被点击,它就不会进入jQuery脚本。任何建议都将不胜感激。
这里是我的链接…
<a href="#" id='add' >Add</a>
<?php foreach($names as $name): ?>
<a href="#" id="<?= $name['id']; ?>" class="league"><?= $name['name']; ?></a>
<?php endforeach; ?>
<section id='main'>Main content</section>
这是我的jQuery脚本…
var app = { // this is a namespace
nav: $('a.league'), // selector
content: $('section#main') // section where id='main'
};
app.putContent = function(content){
this.content.html(content);
}
app.loadPage = function(page){
$.ajax({
url: '../views/security.php',
type: 'GET',
cache: false,
//data:{id: page}, // this works too
data:"id=" + page,
success: function(data){
app.putContent(data);
},
error: function(){
app.putContent('Could not find page.');
}
});
}
app.init = function(){
app.loadPage('add'); // this part works
app.nav.on('click', function(){ // This part(on click) is not working
var page = $($this).attr("id");
app.loadPage(page);
});
}();
改变这一行:
var page = $($this).attr("id");
这:
var page = $(this).attr("id");
提示:
- 使用浏览器检查器/控制台来识别JS中的问题代码