我有一个插件,用word替换文本{headernav}例如,当wordpress页面加载。它在页面首次加载时工作正常,但是如果我导航到另一个页面,{headernav}将显示而不是由javascript替换的html内容。执行JavaScript后点击wordpress永久链接不工作
我试过的东西。
- 我用萤火并发现,当我点击了永久链接,并将其转到页后的javscript被加载。
下面是插件加载的JavaScript代码。
的Javascript:
jQuery(document).ready(function($){//begin document ready function call
//place needed javascript for the plugin elements inside this function call
$.get("wp-content/plugins/elemental/content/headernav.php",function(element){
//the tag to replace
var headernav = "{headernav}";
//create instance of RegExp
var regEx = new RegExp(headernav,"g");
//get the html of the body
var html = $("body").html();
//replace the {headernav} with the html returned by the get method
var newValue = html.replace(regEx,element);
//update the body with the new html value
$("body").html(newValue);
});
//do not place any javascript code on or past this line
});//end document ready function call
所以这个插件加载在永久链接去的页面上呢?通过说{nav}显示,你的意思是{headernav}?另一件事是 – Xeschylus 2015-02-05 13:26:54
。您使用$ .get()的相对路径。你是否和你的第一页在同一根?如果您从example.com转到example.com/post,那么相关链接将被打破,从而导致错误。这又会导致$ .get()失败。 – Xeschylus 2015-02-05 13:30:19
是的,我的意思是{headernav},我忘了编辑。点击固定链接后,您的地址栏中显示的路径是正确的。 http:// localhost/wordpress_dev/adding-an-image-test /你有任何关于在我的JavaScript文件中使用相对链接的建议吗? – 2015-02-05 13:46:01