如何在点击时调用函数
How to call a function on click
我在重新初始化某些代码时遇到问题。
我有一个插件的功能,我需要在点击时重新初始化。我上个月开始学习 JS 和 PHP 所以我仍然对操作我没有写过的代码感到不舒服。
这是我需要重新加载的代码,它是在 PHP 文件中找到的:
/** $return FFGeneralSettings */
public function getGeneralSettings(){
return $this->generalSettings;
}
public function register_shortcodes()
{
add_shortcode('ff', array($this, 'renderShortCode'));
}
public function renderShortCode($attr, $text=null) {
if ($this->prepareProcess()) {
$stream = $this->generalSettings->getStreamById($attr['id']);
if (isset($stream)) {
$settings = new FFStreamSettings($stream);
if ($settings->isPossibleToShow()){
/*ob_start();
include(AS_PLUGIN_DIR . 'views/public.php');
$file_content = ob_get_clean();
echo $file_content;*/
ob_start();
include(AS_PLUGIN_DIR . 'views/public.php');
$output = ob_get_contents();
ob_get_clean();
return $output;
}
}
}
}
我想我会使用 Javascript 来重新初始化上面代码中的操作?我在下面制作的骨架是否正确?
$(document).ready(function(){
$('a').on('click',function(){
//does the magic happen in here?
});
});
感谢您的建议,到目前为止,我从你们那里学到了很多东西!
哦,还有,如果答案在 jQuery 中,我很好。
如果您尝试从 Javascript reload/rerun 一个 PHP 函数,您将需要创建一个 API 端点,您可以从 javascript 或者做一个老式的页面刷新。如果这对您没有意义,请多了解一下 HTTP 请求和响应的基础知识。
"I have a function from a plugin that I need to reinitialize on click"
一切都将取决于您希望此功能执行的操作。你想让它产生一些输出显示在你的页面上吗?你想让它只是 运行 一个服务器端脚本吗?两者都需要您调用 php 页面,该页面将 运行 那个 php 代码,这可能会以 ajax 调用的形式出现,但是您结果会因您的目标而异。
要使用 jquery 进行 ajax 调用,请执行以下操作:
$(document).ready(function(){
$('a').on('click',function(){
// magic happens here!
$.ajax({
url: "yourfile.php",
method: "get",
success: function(result) {
// do something with your result here
console.log(result);
},
});
});
});
看起来你的代码会 return 一些东西 - 所以上面的代码应该在 success = function (result)
部分做一些事情,这可能是将输出结果放入某个元素。
我在重新初始化某些代码时遇到问题。
我有一个插件的功能,我需要在点击时重新初始化。我上个月开始学习 JS 和 PHP 所以我仍然对操作我没有写过的代码感到不舒服。
这是我需要重新加载的代码,它是在 PHP 文件中找到的:
/** $return FFGeneralSettings */
public function getGeneralSettings(){
return $this->generalSettings;
}
public function register_shortcodes()
{
add_shortcode('ff', array($this, 'renderShortCode'));
}
public function renderShortCode($attr, $text=null) {
if ($this->prepareProcess()) {
$stream = $this->generalSettings->getStreamById($attr['id']);
if (isset($stream)) {
$settings = new FFStreamSettings($stream);
if ($settings->isPossibleToShow()){
/*ob_start();
include(AS_PLUGIN_DIR . 'views/public.php');
$file_content = ob_get_clean();
echo $file_content;*/
ob_start();
include(AS_PLUGIN_DIR . 'views/public.php');
$output = ob_get_contents();
ob_get_clean();
return $output;
}
}
}
}
我想我会使用 Javascript 来重新初始化上面代码中的操作?我在下面制作的骨架是否正确?
$(document).ready(function(){
$('a').on('click',function(){
//does the magic happen in here?
});
});
感谢您的建议,到目前为止,我从你们那里学到了很多东西! 哦,还有,如果答案在 jQuery 中,我很好。
如果您尝试从 Javascript reload/rerun 一个 PHP 函数,您将需要创建一个 API 端点,您可以从 javascript 或者做一个老式的页面刷新。如果这对您没有意义,请多了解一下 HTTP 请求和响应的基础知识。
"I have a function from a plugin that I need to reinitialize on click"
一切都将取决于您希望此功能执行的操作。你想让它产生一些输出显示在你的页面上吗?你想让它只是 运行 一个服务器端脚本吗?两者都需要您调用 php 页面,该页面将 运行 那个 php 代码,这可能会以 ajax 调用的形式出现,但是您结果会因您的目标而异。
要使用 jquery 进行 ajax 调用,请执行以下操作:
$(document).ready(function(){
$('a').on('click',function(){
// magic happens here!
$.ajax({
url: "yourfile.php",
method: "get",
success: function(result) {
// do something with your result here
console.log(result);
},
});
});
});
看起来你的代码会 return 一些东西 - 所以上面的代码应该在 success = function (result)
部分做一些事情,这可能是将输出结果放入某个元素。