我想根据单击的按钮加载视图。当然,当第一次登陆页面时,将不会有任何点击,它将载入特定的视图。但是,当用户单击某些按钮中的一个时,将基于所单击的内容将第一个视图替换为该视图。
这是我一直在尝试的。
$(".pagesbutton").click(function(){
$(this).data('clicked', true);
if($('.pagesbutton').data('clicked')) {
buttonpressed = $(this).attr('id');
if(buttonpressed == 'one') {
//will load the view of one
}
else {
//will load the view of two
}
}
else {
//will load the view of default
//it is the first land of the page load when nothing is clicked.
}
});
当第一次加载页面时,jquery仅警告
default
视图。但是之后,当我单击所有按钮时,它不会发出任何警报。 最佳答案
DEMO - JS Fiddle
我认为您的JQuery应该是这样的:
$(document).ready(function() {
$('.theView').prepend('The default view!');
});
$('.pagesbutton').on('click', function() {
$('.theView').empty();
var buttonpressed = $(this).attr('id');
if (buttonpressed == 'one') {
$('.theView').prepend('The view of One!');
}
else {
$('.theView').prepend('The view of Two!');
}
});
<div class="theView">this is one view bla bla bla with css class and id</div>
关于javascript - 检查单击的内容,然后进行条件检查;如果没有,则进行条件检查,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30364107/