从 php echo 调用 javascript 图像不透明度函数

发布于 2024-11-11 03:26:42 字数 1689 浏览 2 评论 0原文

我已经做到了,当您将光标悬停在图像按钮上时,它开始褪色,当您将光标移出时,它会恢复到原来的不透明度。问题是,当我在正文内容周围添加 php 标签并回显表格时,它不再起作用。感谢您提前提供帮助。

这是代码:

<html>
<head>
<script type="text/javascript">
function SetOpacity(elem, opacityAsInt)
{
    var opacityAsDecimal = opacityAsInt;

    if (opacityAsInt > 100)
        opacityAsInt = opacityAsDecimal = 100; 
    else if (opacityAsInt < 0)
        opacityAsInt = opacityAsDecimal = 0; 

    opacityAsDecimal /= 100;
    if (opacityAsInt < 1)
        opacityAsInt = 1; // IE7 bug, text smoothing cuts out if 0

    elem.style.opacity = opacityAsDecimal;
    elem.style.filter  = "alpha(opacity=" + opacityAsInt + ")";
}

function FadeOpacity(elemId, fromOpacity, toOpacity, time, fps)
{
    var steps = Math.ceil(fps * (time / 1000));
    var delta = (toOpacity - fromOpacity) / steps;

    FadeOpacityStep(elemId, 0, steps, fromOpacity, delta, (time / steps));
}

function FadeOpacityStep(elemId, stepNum, steps, fromOpacity, delta, timePerStep)
{
    SetOpacity(document.getElementById(elemId), Math.round(parseInt(fromOpacity) + (delta * stepNum)));

    if (stepNum < steps)
        setTimeout("FadeOpacityStep('" + elemId + "', " + (stepNum+1) + ", " + steps + ", " + fromOpacity + ", " + delta + ", " + timePerStep + ");", timePerStep);
}

</script>
</head>


<body>
<?php

echo"
<form action='opacity.php' method='post'>

<input type='image' name='blue' id='ImgAkxl2' value='blue' src='streetfighter.jpg'
onmouseover='FadeOpacity('ImgAkxl2', 100, 70, 250 , 24)'
onmouseout ='FadeOpacity('ImgAkxl2', 70, 100, 250 , 24)'



/>
</form>


";

?>
</body>
</html> 

I've made it so that when you hover the cursor over the image button it starts fading and when you move the cursor out, it goes back to original opacity. The problem is that when I add php tags around the content in the body and echo the table, it no longer works. Thanks for advance for the help.

Here's the code:

<html>
<head>
<script type="text/javascript">
function SetOpacity(elem, opacityAsInt)
{
    var opacityAsDecimal = opacityAsInt;

    if (opacityAsInt > 100)
        opacityAsInt = opacityAsDecimal = 100; 
    else if (opacityAsInt < 0)
        opacityAsInt = opacityAsDecimal = 0; 

    opacityAsDecimal /= 100;
    if (opacityAsInt < 1)
        opacityAsInt = 1; // IE7 bug, text smoothing cuts out if 0

    elem.style.opacity = opacityAsDecimal;
    elem.style.filter  = "alpha(opacity=" + opacityAsInt + ")";
}

function FadeOpacity(elemId, fromOpacity, toOpacity, time, fps)
{
    var steps = Math.ceil(fps * (time / 1000));
    var delta = (toOpacity - fromOpacity) / steps;

    FadeOpacityStep(elemId, 0, steps, fromOpacity, delta, (time / steps));
}

function FadeOpacityStep(elemId, stepNum, steps, fromOpacity, delta, timePerStep)
{
    SetOpacity(document.getElementById(elemId), Math.round(parseInt(fromOpacity) + (delta * stepNum)));

    if (stepNum < steps)
        setTimeout("FadeOpacityStep('" + elemId + "', " + (stepNum+1) + ", " + steps + ", " + fromOpacity + ", " + delta + ", " + timePerStep + ");", timePerStep);
}

</script>
</head>


<body>
<?php

echo"
<form action='opacity.php' method='post'>

<input type='image' name='blue' id='ImgAkxl2' value='blue' src='streetfighter.jpg'
onmouseover='FadeOpacity('ImgAkxl2', 100, 70, 250 , 24)'
onmouseout ='FadeOpacity('ImgAkxl2', 70, 100, 250 , 24)'



/>
</form>


";

?>
</body>
</html> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

刘备忘录 2024-11-18 03:26:42

您需要转义 onmouseover 和 onmouseout 事件中的内部引号

<input type='image' name='blue' id='ImgAkxl2' value='blue' src='streetfighter.jpg'
onmouseover='FadeOpacity(\'ImgAkxl2\', 100, 70, 250 , 24)'
onmouseout ='FadeOpacity(\'ImgAkxl2\', 70, 100, 250 , 24)'
/>

You need to escape the internal quotes in your onmouseover and onmouseout events

<input type='image' name='blue' id='ImgAkxl2' value='blue' src='streetfighter.jpg'
onmouseover='FadeOpacity(\'ImgAkxl2\', 100, 70, 250 , 24)'
onmouseout ='FadeOpacity(\'ImgAkxl2\', 70, 100, 250 , 24)'
/>
葵雨 2024-11-18 03:26:42
onmouseover='FadeOpacity('ImgAkxl2', 100, 70, 250 , 24)'
onmouseout ='FadeOpacity('ImgAkxl2', 70, 100, 250 , 24)'

变成

onmouseover='FadeOpacity(\"ImgAkxl2\", 100, 70, 250 , 24)'
onmouseout ='FadeOpacity(\"ImgAkxl2\", 70, 100, 250 , 24)'

,你的问题就解决了! :)

在属性值中使用相同类型的引号会关闭该值的部分,如果这有意义的话?

onmouseover='FadeOpacity('ImgAkxl2', 100, 70, 250 , 24)'
onmouseout ='FadeOpacity('ImgAkxl2', 70, 100, 250 , 24)'

becomes

onmouseover='FadeOpacity(\"ImgAkxl2\", 100, 70, 250 , 24)'
onmouseout ='FadeOpacity(\"ImgAkxl2\", 70, 100, 250 , 24)'

and you have your problem solved! :)

Having the same type of quote inside an attribute's value closes the value's section, if that makes any sense?

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