前几天我写了一个简单的 WordPress 插件来保存帖子并显示预览。这是javascript。
jQuery(document).ready(function($){
if (document.cookie.indexOf("previewCookie") >= 0){
//expires added for IE
document.cookie="previewCookie=true; max-age=0;expires=0;path=/wp-admin/";
//quickPreviewOption is set in quick-preview.php
var previewURL = document.getElementById('post-preview');
if(quickPreviewOption === 'current'){
window.location = previewURL;
}
if(quickPreviewOption === 'new'){
window.open(previewURL,"wp_PostPreview","","true");
}
}
$(document).keydown(function(e){
if((e.ctrlKey || e.metaKey) && e.which == 83){
//Find #save post if it's a draft. If post is published, #save-post doesn't exist.
if($('#save-post').length){
$('#save-post').click();
}
else if($('#publish').length){
$('#publish').click();
}
//Sets a cookie to open the preview on page refresh. Saving a post auotmatically refreshes the page.
document.cookie = "previewCookie = true;max-age = 60;path=/wp-admin/";
return false;
}
});
});
这在 wordpress 中使用“html”编辑器时有效。但是,使用 WordPress 中的“可视化”编辑器,我无法触发任何 keydown 功能。不是 ctrl-s,ctrl-q 等。我不知道是什么阻止了它,我在源代码中找不到它。我尝试取消绑定所有 keydown 事件,然后只重新绑定我的,它没有取消绑定 WordPress keydown 事件。有人有想法么?
这是快速预览插件的链接,以防万一。http://wordpress.org/extend/plugins/quick-preview/