作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我在重新初始化某些代码时遇到问题。
我有一个插件的功能,我需要在点击时重新初始化。我上个月开始学习 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 中,我也很好。
最佳答案
“我有一个插件的功能,我需要在点击时重新初始化”
一切都取决于你希望这个函数做什么。你想让它产生一些输出显示在你的页面上吗?你想让它只运行服务器端脚本吗?两者都需要您调用将运行该 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);
},
});
});
});
看起来你的代码会返回一些东西 - 所以上面的代码应该在 success = function (result)
部分做一些事情,这可能是将输出结果放入某个元素。
关于javascript - 如何在点击时调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30294706/
我是一名优秀的程序员,十分优秀!