加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 资源网站 > 资源 > 正文

javascript – 如何在同一窗口中打开WordPress’预览’链接?

发布时间:2020-12-14 22:46:01 所属栏目:资源 来源:网络整理
导读:我在Wordpress Stack Exchange上询问了How can I make the “Preview Post” button save and preview in the same window?,但对于Stack Overflow来说这可能是一个更好的问题,因为它与编码更直接相关. WordPress有一个框,允许您保存,预览和发布您的博客帖子

我在Wordpress Stack Exchange上询问了How can I make the “Preview Post” button save and preview in the same window?,但对于Stack Overflow来说这可能是一个更好的问题,因为它与编码更直接相关.

WordPress有一个框,允许您保存,预览和发布您的博客帖子:

Picture.png http://img854.imageshack.us/img854/7986/picturek.png

“预览”按钮实际上是一个设为按钮的链接:

我的问题是我似乎无法弄清楚如何在当前窗口中打开该链接.注意target =“wp-preview”部分.我试图摆脱那个部分,但我认为可能有另一个函数绑定到该元素,因为我真的无法让它在当前选项卡/窗口中打开,即使解除绑定并删除目标属性.

我正在运行以下代码作为插件的一部分(您可以在下面看到有关如何将其作为插件运行的更多信息),但也可以将其复制并粘贴到Chrome或Firefox的控制台中以自行测试甚至修改Wordpress.请注意,在测试时,您需要在自己的函数中使用jQuery而不是$,因为Wordpress使用noconflict方法,但是下面的代码工作正常.

//select the node and cache the selection into a variable
var $node = jQuery('a.preview'); 

//add a 1px dotted outline to show we have the right element
$node.css('outline','1px dotted red'); 

//show current target
console.log($node.prop('target')); 
//show if anything is bound - nothing is for me ('undefined')
console.log($node.data('events')); 

//remove anything bound to it
$node.unbind(); 
//set target to _self (current window),just in case
$node.prop('target','_self'); 
//the remove the target attribute all together
$node.removeAttr('target'); 

//clone the node
var $clone = $node.clone(); 
//change the text to new
$clone.text('new'); 
//remove target from clone
$clone.removeAttr('target'); 
//unbind the clone
$clone.unbind(); 
//insert the clone after the original node
$node.after($clone); 

//show current target - now shows undefined for me
console.log($node.prop('target'));
//show if anything is bound - still 'undefined'
console.log($node.data('events'));

这是您将代码用于主题或插件的方式:

// set up an action to set a function to run in the wp admin_footer
add_action('admin_footer','my_admin_footer_script',9999);

这是添加javascript的函数:

//this function can then be used to add javascript code to the footer

function my_admin_footer_script(){ ?>