javascript - 如何让 Wordpress 'Preview' 链接在同一窗口中打开?

标签 javascript jquery html wordpress

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

Wordpress 有一个框,允许您保存、预览和发布您的博客文章:

Picture.png

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

<a tabindex="4" id="post-preview" target="wp-preview"
href="/?p=67&amp;preview=true" class="preview button">Preview</a>

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

我将以下代码作为插件的一部分运行(您可以在下面查看有关如何将其作为插件运行的更多信息),但也可以将其复制并粘贴到 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(){ ?>
    <script type="text/javascript">
    jQuery(function($){
     (above js code here)
    });
    </script>


    <?php
}

这是我最终得到的结果。如果我单击“测试”链接,它将在同一窗口中打开。如果我单击“预览”链接,它仍会在新选项卡中打开。

Picture.png

ps:我使用的是Things you may not know about jQuery中的方法检查绑定(bind)事件,我没有发现任何绑定(bind),我相信 Wordpress 主要使用 jQuery,所以我认为这不会与另一个事件处理程序绑定(bind)。

最佳答案

你可以试试这个:

jQuery('.preview.button').click(function(e){
    window.location.href = this.href;
    return false;
});

在 Chrome 检查器中工作。

关于javascript - 如何让 Wordpress 'Preview' 链接在同一窗口中打开?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8380816/

相关文章:

Javascript 将我的 php var 0000001000 变为 512

javascript - jQuery : How to remove class for all element on click?

javascript - 页面加载后添加图像

javascript - 是否可以将光标锁定/聚焦到文本区域的最后一行?

javascript - 为什么 smooth scrolling.js 抛出 "$target.offset(...) is undefined"错误?

javascript - 有人可以解释为什么这个正则表达式与不同的正则表达式风格匹配不同吗?

javascript - angularJS - 表单中的重复输入

javascript - 使用复选框条件激活单选按钮

javascript - Gmaps.js 路线与时间

php - 抓取动态天气幻灯片