我有一个带有div容器的主容器php文件。最初,在加载页面时,div容器默认没有任何内容或html子项。当用户单击导航栏中的导航项时,我使用JQuery将php文件加载到此div容器。
landingpage.php
<?php
require_once 'navbar.php';
?>
<!DOCTYPE html>
<html>
<head>
<title>Admin | Dashboard</title>
<link rel="stylesheet" href="css/dashboard_admin.css">
</head>
<body>
<div class="wrapper">
<!-- CONTENT CONTAINER's content depends on what was clicked on navbar -->
<div class="container" id="content_container">
<div class="div_dashboardLabel" id="dashboard_Label">
<h2 id="label_Dashboard">Admin Dashboard</h2>
</div>
</div>
<!-- END OF CONTENT CONTAINER-->
</div>
<!-- end of wrapper-->
<script src="js/jquery-3.3.1.js"></script>
<script type="text/javascript">
var user = '<?php echo json_encode($user);?>';
var role = '<?php echo json_encode($role); ?>';
</script>
<script src="js/landingpage.js"></script>
</body>
</html>
landingpage.js
/* GLOBAL VARIABLES WITHIN THIS DOCUMENT*/
var div_content_container = $("#content_container");
var navitem_dashboard = $("#admin_dashboard");
var navitem_admin_account_management = $("#admin_accountmanagement");
var userObj = JSON.parse(user);
var roleObj = JSON.parse(role);
var logout = $('#logout');
/* END */
$(document).ready(function(){
loadDashboard(); // dashboard is loaded as default view.
navitem_admin_account_management.on("click",loadAccountManagement);
});
function loadDashboard() {
var url_admin_dashboard = 'view/admin_dashboard.php';
var url_teacher_dashboard = 'view/teacher_dashboard.php';
var url_student_dashboard = 'view/student_dashboard.php';
div_content_container.html('');
if (roleObj.rolename === 'Administrator') {
div_content_container.load(url_admin_dashboard);
} else if (roleObj.rolename === 'Teacher') {
div_content_container.load(url_teacher_dashboard);
} else if (roleObj.rolename === 'Student') {
div_content_container.load(url_student_dashboard);
}
}
function loadAccountManagement(){
var url = 'view/admin_accountmanagement.php';
div_content_container.html('');
div_content_container.load(url);
}
对于前端使用 landingpage.js 的 landingpage.php ,一切正常。没问题。
问题是在admin_accountmanagement.php
中加载了 div_content_container
文件。 admin_account_management.php 的JS似乎没有绑定到元素:
function loadAccountManagement(){
var url = 'view/admin_accountmanagement.php'; // has its JS file
div_content_container.html('');
div_content_container.load(url);
}
例如,我们选择url
'view/admin_accountmanagement.php'
当用户点击导航栏项帐户管理时,此页面会在div_content_container
内加载
,如
<div class="wrapper">
<!-- CONTENT CONTAINER's content depends on what was clicked on navbar -->
<div class="container" id="content_container">
<!-- view/admin_accountmanagement.php loaded here -->
</div>
<!-- END OF CONTENT CONTAINER-->
</div>
显示页面或替换div_content_container
中包含的当前元素没有问题。问题是,当 view / admin_accountmanagement.php时,附加到 view / admin_accountmanagement.php 的 JS文件似乎不适用页面已加载到div_content_container
view / admin_accountmanagement.php 已加载,但点击事件绑定到 view / admin_accountmanagement.php 的元素工作。我知道这是因为我试图在alert()
$(document).ready()
消息
admin_accountmanagement.php
<body>
<div>
<button class="button" id="btn_AddUser">
Add New User
</button>
</div>
<script src="js/admin_accountmanagement.js"></script> <!-- this JS doesn't seem to get binded when this page is loaded in the div_content_container -->
</body>
admin_accountmanagement.js
var btn_add_user = $('#btn_AddUser');
$(document).ready(function(){
alert("TEST"); //doesn't work no alert message.
btn_add_user.on("click",test); //doesn't work no alert message
});
function test(){
alert("testing"); //doesn't work. no alert message
}
我只能显示页面,但是当我点击<button id="btn_AddUser">
时没有任何反应。
考虑到我将页面加载到div_content_container
的方式,我如何解决这个问题?
提前致谢。
答案 0 :(得分:2)
将admin_accountmanagement.js
更改为
var btn_add_user = $('#btn_AddUser');
alert('Account management js loaded'); // this should show alert
$(document).on("click",btn_add_user,test);
function test(){
alert("testing"); //doesn't work. no alert message
}
此方法有效,因为绑定不依赖于“文档就绪”,因为第一次加载父页时已经触发了文档就绪
答案 1 :(得分:0)
@jordan我同意@Magnus Eriksson,因为最好绑定它on('event','selector','function')
,但在.off()
播放.on()
内容时使用dynamic
这可能导致{b>多个绑定function
。如果您仍无法使绑定事件发挥作用,您可以尝试在加载内容之后在页面的头部注入.js文件,例如:$('head').append('<script src="admin_accountmanagement.js"></script>')
。使用load()
中的js
功能绑定click event
。
或者,您使用以下代码段:
function LoadContent()
{
var url = 'view/admin_accountmanagement.php';
var jssrc = 'js/admin_accountmanagement.js';
div_content_container.html('');
div_content_container.load(url);
if($('head').find('*[src="'+jssrc+'"]').length==0)
{
//the below approach have some consequences related to it as you will not be able to see the injected js in your developers tool's Sources(in chrome).
$('head').append('<script src="'+jssrc+'"></script>');
}
}
然后在.js
上看起来像这样:
var btn_add_user = null;
$(window).load(function(){
alert("TEST");
btn_add_user = $('#btn_AddUser');
btn_add_user.off('click').on("click",test); //use .off('event') if load() is being called multiple times.
});
function test(){
alert("testing");
}