dpzzkfb1244 2012-11-05 09:13
浏览 44
已采纳

如何在CakePHP中实现JavaScript onmouseover?

What I want to do is convert the following to work with CakePHP:

<a href="URL ADDRESS"><img src="URL OF THE FIRST IMAGE GOES HERE" onmouseover="this.src='URL OF THE SECOND IMAGE GOES HERE'" onmouseout="this.src='URL OF THE FIRST IMAGE GOES HERE'" /></a>

I have the following so far:

<?php echo $this->Html->image("animalhealth.png", array(
    "alt" => "Animal Health",
    "onmouseover" => "this.src='animalhealthhover.png'",
    "onmouseout" => "this.src='animalhealth.png'",
    'url' => array('controller' => 'records', 'action' => 'index'
    )
)); ?>

The problem are the onmouseover & onmouseout event lines. I need to tell cake to somehow use the helper method otherwise it just selects no image. I don't want to have to put the entire address in as the is a navigation menu and there will be multiple instances of the app at different locations.

  • 写回答

4条回答 默认 最新

  • dongzhang4301 2012-11-06 05:02
    关注

    I have managed to build a work around using CSS.

    Made a custom image for the button Icon.

    This is what goes in the view/layout page:

    <ul class="menu">
    <li>
                            <div id="button_name"><?php
                        echo $this->Html->image("name_of_img.png", array(
                            "alt" => "Animal Health",
                            'url' => array('controller' => 'controllerName', 'action' => 'index')));
                        ?>
    <?php echo $this->Html->link(__('Link Text'), array('controller' => 'controllerName', 'action' => 'index')); ?> 
                            </div>
                        </li> 
    </ul>
    

    If your not using cakePHP you could do it like this in a normal HTML page:

    <ul class="menu">
    <li>
                            <div id="button_name"><a href="/path/to/page.html"><img src="/path/to/img/imagename.png" alt="Animal Health" /></a><a href="/path/to/page.html">Animal Health</a> 
                            </div>
                        </li>
    <ul>
    

    This Makes both the text and the icon clickable.

    Then the CSS:

    .menu li {padding:0px; border: 0px; margin: 0px; list-style: none;
              float: left; margin-left: 0px; display: block; height: 36px;} //remove any stlying and set up for the menu.
    
    #button_name{background-color: darkorange;
                   float: left;
                  margin-right: 5px;
                   margin-top: 1px;
                   margin-bottom: 0px;
                   padding: 1px 3px 1px 3px;
                   -webkit-border-radius: 5px 5px 0px 0px;
                   border-radius: 5px 5px 0px 0px;
                   border: 1px black;
                   text-align: right;
                   color: #6495ED;
                   font-weight: bold;
                   -webkit-transition: all 1000ms;
                   -moz-transition: all 1000ms;
                   -o-transition: all 1000ms;
                   -ms-transition: all 1000ms;
                   transition: all 1000ms;}
    #button_name a {
        -webkit-transition: all 1000ms;
        -moz-transition: all 1000ms;
        -o-transition: all 1000ms;
        -ms-transition: all 1000ms;
        transition: all 1000ms;
        font-weight: bold;
        color: #6495ED; 
    }
        #button_name:hover
     {background-color: #6495ED;}
        #button_name:hover a // VERY IMPORTANT see note
    {
            font-weight: bold;
            color: darkorange;}
    

    This makes a nice button with 2 rounded top corners. When you hover over the button anywhere the background and text colours transition between each other. ie: text goes from blue to orange and background goes from orange to blue.

    Note about #button_name:hover a: You must set it as specified if you set it as "button_name a:hover" the text will stay the same color as the background.

    Hopefully this will help someone else.

    Still keen to hear any ideas about doing it with the JS.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档