javascript - 网页中的SVG元素无法绑定jQuery事件?
伊谢尔伦
伊谢尔伦 2017-04-11 10:13:46
[JavaScript讨论组]

目的

鼠标滑过SVG元素时SVG随机伸缩,后恢复原状(各100ms)

实现代码

<style>
p{
    transform: rotate(180deg);//旋转外层p 180°使SVG伸缩方向上下颠倒
    transform-origin: 50% 50%;
    display: inline-block;
}

g.rect_group rect{
    transition: all 0.1s ease;//定义SVG变换时间
}
</style>
<body>
<p>
    <svg height="300" width="300">
        <g class="rect_group" fill="#B9D6E8">//SVG组
            <rect height="100" width="20"/>
            <rect x="25" height="100" width="20"/>
            <rect x="50" height="100" width="20"/>
            <rect x="75" height="100" width="20"/>
            <rect x="100" height="100" width="20"/>
        </g>
      Sorry, your browser does not support inline SVG.
    </svg>
<p>
 <script type="text/javascript">
     $(document).ready(function(){
         var randomHeight=function($ele){
             $ele.data("this_height",$ele.attr("height"));//写入原高度到元素的.data对象
             $ele.attr("height",Math.floor((Math.random()+0.5)*parseInt($ele.data("this_height"))));//随机伸长/缩短
             setTimeout(function(){
                 $ele.attr("height",$ele.data("this_height"));
             },100);//100ms后恢复原状
         }
         $(".rect_group rect").on("mouseover",function(){//注册时间
             randomHeight($(this));
         });
     })
 </script>
</body>

问题

这段代码在本地实现得很好,但搬到服务器上就无法绑定,元素的event listener也为空,求解?

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

全部回复(2)
阿神

jquery引入了吗

巴扎黑
<style>
p{
    transform: rotate(180deg);//旋转外层p 180°使SVG伸缩方向上下颠倒
    transform-origin: 50% 50%;
    display: inline-block;
}

g.rect_group rect{
    transition: all 0.1s ease;//定义SVG变换时间
}
</style>
<body>
<script src="http://cdn.bootcss.com/jquery/1.12.3/jquery.js"></script>
<p>
    <svg height="300" width="300">
        <g class="rect_group" fill="#B9D6E8">//SVG组
            <rect height="100" width="20"/>
            <rect x="25" height="100" width="20"/>
            <rect x="50" height="100" width="20"/>
            <rect x="75" height="100" width="20"/>
            <rect x="100" height="100" width="20"/>
        </g>
      Sorry, your browser does not support inline SVG.
    </svg>
<p>
 <script type="text/javascript">
     $(document).ready(function(){
         var randomHeight=function($ele){
             $ele.data("this_height",$ele.attr("height"));//写入原高度到元素的.data对象
             $ele.attr("height",Math.floor((Math.random()+0.5)*parseInt($ele.data("this_height"))));//随机伸长/缩短
             setTimeout(function(){
                 $ele.attr("height",$ele.data("this_height"));
             },100);//100ms后恢复原状
         }
         $(".rect_group rect").on("mouseover",function(){//注册时间
             randomHeight($(this));
         });
     })
 </script>
</body>

在本地执行有效果
你说的在你服务器上没效果我们也看不到啊

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号