Wordpress 和 Javascript 鼠标悬停

发布于 2024-12-14 20:21:54 字数 1067 浏览 2 评论 0原文

我正在尝试开发一个插件,当用户单击评论作者的姓名时,该插件会显示网站屏幕截图。 当只有一条评论时,所有内容都有效,但当有多于一条评论时,脚本将不起作用。 我认为问题在于为每个评论调用和发布的变量的名称。 但不知道如何动态改变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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逆光下的微笑 2024-12-21 20:21:54

我将大部分评论移至此答案:

要动态调用变量,请将函数更改为如下所示:

function MouseChange( img , imageSrc ) // img will be the tag name, and imageSrc will be the URL of the image
{
    document[img].src = imageSrc;
}

要调用此变量,您将执行以下操作:

<?php $blogPostID = ???;// I don't know how to get blog post ID of the top of my head ?>
<script type="text/javascript"><!--
    var Img<?= $blogPostID ?>On = new Image();
    Img<?= $blogPostID ?>On.src = "<?php echo $urlnohttp;?>";
    var Img<?= $blogPostID ?>Off = new Image();
    Img<?= $blogPostID ?>Off.src = "<?php bloginfo('url');?>/wp-content/plugins/[...]/control_play.png";
// this closes the HTML comment: --></script>

<a href="<?php echo $commenturl ?>" onMouseOver="MouseChange( 'Img<?= $blogPostID ?>' , Img<?= $blogPostID ?>On.src )" onMouseOut = "MouseChange( 'Img<?= $blogPostID ?>' , Img<?= $blogPostID ?>Off.src )" ><?php echo $author ?>
    <img src="<?php bloginfo('url');?>/wp-content/plugins/[...]/control_play.png" border="0" name="Img<?= $blogPostID ?>" />
</a>

所以我所做的是将所有数字替换为博客文章 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:

function MouseChange( img , imageSrc ) // img will be the tag name, and imageSrc will be the URL of the image
{
    document[img].src = imageSrc;
}

To call this you will do this:

<?php $blogPostID = ???;// I don't know how to get blog post ID of the top of my head ?>
<script type="text/javascript"><!--
    var Img<?= $blogPostID ?>On = new Image();
    Img<?= $blogPostID ?>On.src = "<?php echo $urlnohttp;?>";
    var Img<?= $blogPostID ?>Off = new Image();
    Img<?= $blogPostID ?>Off.src = "<?php bloginfo('url');?>/wp-content/plugins/[...]/control_play.png";
// this closes the HTML comment: --></script>

<a href="<?php echo $commenturl ?>" onMouseOver="MouseChange( 'Img<?= $blogPostID ?>' , Img<?= $blogPostID ?>On.src )" onMouseOut = "MouseChange( 'Img<?= $blogPostID ?>' , Img<?= $blogPostID ?>Off.src )" ><?php echo $author ?>
    <img src="<?php bloginfo('url');?>/wp-content/plugins/[...]/control_play.png" border="0" name="Img<?= $blogPostID ?>" />
</a>

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#".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文