[英]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.