在我的wordpress插件文件中,我有一段包含javascript的代码
function javascript_file-exmp() {
$src = plugins_url(\'test.js\', __FILE__);
wp_register_script( \'links\', $src );
wp_enqueue_script( \'links\' );
wp_enqueue_script( \'jquery\' );
}
add_action(\'init\',\'javascript_file-exmp\');
测试。js文件有点长。
if (!document.layers)
document.write(\'<div id="divStayTopLeft" style="position:absolute">\')
<layer id="divStayTopLeft">
<!--EDIT BELOW CODE TO YOUR OWN MENU-->
<table border="1" width="130" cellspacing="0" cellpadding="0">
<tr>
<td width="100%" bgcolor="#FFFFCC">
<p align="center"><b><font size="4">Menu</font></b></td>
</tr>
<tr>
<td width="100%" bgcolor="#FFFFFF">
<p align="left"> <a href="http://www.dynamicdrive.com">Dynamic Drive</a><br>
<a href="http://www.dynamicdrive.com/new.htm">What\'s New</a><br>
<a href="http://www.dynamicdrive.com/hot.htm">What\'s Hot</a><br>
<a href="http://www.dynamicdrive.com/faqs.htm">FAQs</a><br>
<a href="http://www.dynamicdrive.com/morezone/">More Zone</a></td>
</tr>
</table>
<!--END OF EDIT-->
</layer>
/*
Floating Menu script- Roy Whittle (http://www.javascript-fx.com/)
Script featured on/available at http://www.dynamicdrive.com/
This notice must stay intact for use
*/
//Enter "frombottom" or "fromtop"
var verticalpos="frombottom"
if (!document.layers)
document.write(\'</div>\')
function JSFX_FloatTopDiv()
{
var startX = 3,
startY = 150;
var ns = (navigator.appName.indexOf("Netscape") != -1);
var d = document;
function ml(id)
{
var el=d.getElementById?d.getElementById(id):d.all?d.all[id]:d.layers[id];
if(d.layers)el.style=el;
el.sP=function(x,y){this.style.left=x;this.style.top=y;};
el.x = startX;
if (verticalpos=="fromtop")
el.y = startY;
else{
el.y = ns ? pageYOffset + innerHeight : document.body.scrollTop + document.body.clientHeight;
el.y -= startY;
}
return el;
}
window.stayTopLeft=function()
{
if (verticalpos=="fromtop"){
var pY = ns ? pageYOffset : document.body.scrollTop;
ftlObj.y += (pY + startY - ftlObj.y)/8;
}
else{
var pY = ns ? pageYOffset + innerHeight : document.body.scrollTop + document.body.clientHeight;
ftlObj.y += (pY - startY - ftlObj.y)/8;
}
ftlObj.sP(ftlObj.x, ftlObj.y);
setTimeout("stayTopLeft()", 10);
}
ftlObj = ml("divStayTopLeft");
stayTopLeft();
}
JSFX_FloatTopDiv();
我正在尝试将此测试包括在内。js文件,但我的博客上什么都没发生。如何在插件中包含此文件?我知道这可能与html标记或脚本标记有关。但是哪一个呢?
谢谢
最合适的回答,由SO网友:James VanDyke 整理而成
首先,应该使用wp\\u enqueue\\u脚本加载脚本。
add_action( \'wp_enqueue_scripts\' , \'javascript_file-exmp\' );
其次,请确保同时注册脚本,然后将其排入队列
Register:
wp_register_script( \'links\', plugins_url( \'test.js\', __FILE__ ), array( \'jquery\' ), \'1.0\', true );
1.0
是版本,主要用于在更新时避免缓存打开。
true
控制是否可以从页面底部而不是在<head>
标签
Enqueue
wp_enqueue_script( \'links\' );
最后,如上所述,您应该真正将HTML与javascript分开。