Wordpress 和 Javascript 鼠标悬停
我正在尝试开发一个插件,当用户单击评论作者的姓名时,该插件会显示网站屏幕截图。 当只有一条评论时,所有内容都有效,但当有多于一条评论时,脚本将不起作用。 我认为问题在于为每个评论调用和发布的变量的名称。 但不知道如何动态改变JS变量的名称以及如何动态调用它。
这是鼠标悬停的代码(在标题中)
<script type="text/javascript">
function MOver(picimage)
{
Picture_Over = eval(picimage +"On.src")
document[picimage].src = Picture_Over
}
function MOut(picimage)
{
Picture_Out = eval(picimage +"Off.src")
document[picimage].src = Picture_Out
}
-->
</script>
然后这是显示鼠标悬停的代码:
<script type="text/javascript"><!--
var Img2On = new Image();
Img2On.src = "<?php echo $urlnohttp;?>";
var Img2Off = new Image();
Img2Off.src = "<?php bloginfo('url');?>/wp-content/plugins/[...]/control_play.png";
</script>
<a href="<?php echo $commenturl ?>" onMouseOver="MOver('Img2')" onMouseOut = "MOut('Img2')" ><?php echo $author ?> <img src="<?php bloginfo('url');?>/wp-content/plugins/[...]/control_play.png" border="0" name="Img2"></img></a>
我认为问题在于“Img2”名称不唯一。
I'm trying to develop a plugin that shows website screenshot, when a user clicks on the name of the comment's author.
All works when there is just one comment, but when are more than one comment the script doesn't work.
I think the problem is in the name of the variable called and posted for each comment.
But I don't know how to dynamically change the name of the JS variable and how to dynamically call it.
This is the code for mouseover (in the header)
<script type="text/javascript">
function MOver(picimage)
{
Picture_Over = eval(picimage +"On.src")
document[picimage].src = Picture_Over
}
function MOut(picimage)
{
Picture_Out = eval(picimage +"Off.src")
document[picimage].src = Picture_Out
}
-->
</script>
Then this is the code to show the mouseover:
<script type="text/javascript"><!--
var Img2On = new Image();
Img2On.src = "<?php echo $urlnohttp;?>";
var Img2Off = new Image();
Img2Off.src = "<?php bloginfo('url');?>/wp-content/plugins/[...]/control_play.png";
</script>
<a href="<?php echo $commenturl ?>" onMouseOver="MOver('Img2')" onMouseOut = "MOut('Img2')" ><?php echo $author ?> <img src="<?php bloginfo('url');?>/wp-content/plugins/[...]/control_play.png" border="0" name="Img2"></img></a>
I think the problem is in the "Img2" name that isn't unique.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将大部分评论移至此答案:
要动态调用变量,请将函数更改为如下所示:
要调用此变量,您将执行以下操作:
所以我所做的是将所有数字替换为博客文章 ID,然后你可以[相对]确定它是唯一的,只要页面的其他部分没有生成名为“Img#”的东西。
I am moving most of my comment over to this answer:
To dynamically call a variable, change the function to look like this:
To call this you will do this:
So what I did was replace all the numbers with the blog post ID, then you can be [relatively] sure it is unique, as long as no other parts of the page makes things called "Img#".