[英]Best way to execute external Javascript function from HTML element's onclick event returned from an Ajax call
index.php:
<html>
<head>
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<div id="box"></div>
<div onclick="getContent()">Open Window</div>
</body>
</html>
script.js:
function getContent() {
//ajax call that returns the html from content.php and places it in the box div
}
function sayHello() {
console.log('Hello');
}
function sayGoodBye() {
console.log('Good Bye');
}
content.php:
<div onclick="sayHello()">Say Hello</div>
<div onclick="sayGoodBye()">Say Good Bye</div>
使sayWello()和sayGoodBye()函数正常工作的正确方法是什么? 目前,他们什么也没做。
请将文件index.php,script.js,content.php保留在同一文件夹中。
index.php
<html>
<head>
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<div id="box"></div>
<div onclick="getContent()">Open Window</div>
</body>
</html>
script.js
function getContent() {
//ajax call that returns the html from content.php and places it in the box div
$.ajax({
url: 'content.php',
success: function(data) {
$('#box').html(data);
}
});
}
function sayHello() {
console.log('Hello');
}
function sayGoodBye() {
console.log('Good Bye');
}
content.php
<?php
$strContent="";
$strContent.="<div onclick='sayHello()'>Say Hello</div>";
$strContent.="<div onclick='sayGoodBye()'>Say Good Bye</div>";
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.