简体   繁体   English

悬停元素时如何将javascript链接到元素更改图片并显示第三个元素

[英]How to chain javascript to when hovering element to change picture of an element and display block third element

Hello guys i am trying to make something like a slider, i have 4 buttons which when pressed i want to change the main picture and show the content for each picture, after a bit of browsing and testing i managed the get the image to change on hover however i am having difficulty getting the content div to change i was trying to use the display none for the other content divs and block for the active one however i can't chain the whole thing, the best i could do was this 大家好,我正在尝试制作类似滑块的内容,我有4个按钮,当我按下这些按钮时,我想更改主图片并显示每张图片的内容,经过一些浏览和测试后,我设法更改了图片悬停,但是我很难更改内容div,我试图不对其他内容div使用显示,而对活动的div进行阻止,但是我无法将整个事情链接起来,我能做的最好的就是

    $('#changeImage').attr('src', 'pic1.png');
  },
       function() {
    $(".content.one").css("display", "block");
   },
        function() {
    $(".content.two").css("display", "none");
   },
        function() {
    $(".content.three").css("display", "none");
   },
        function() {
    $(".content.four").css("display", "none");
   }
)

however only the .content.one changes and only when i move my mouse out. 但是,只有.content.one会更改,并且只有当我将鼠标移出时才更改。 I hope i made my self clear my English is not that good if i can help clarify anything feel free to ask. 我希望我能说清楚自己的英语水平,如果我能帮助澄清任何想问的问题的话,我的英语水平不是很好。

here is the whole java script 这是整个java脚本

<script type="text/javascript">
$(document).ready(function() {
    $("ul li:nth-child(1)").hover(
  function () {     
    $('#changeImage').attr('src', 'pic1.png');
  },
       function() {
    $(".content.one").css("display", "block");
   },
        function() {
    $(".content.two").css("display", "none");
   },
        function() {
    $(".content.three").css("display", "none");
   },
        function() {
    $(".content.four").css("display", "none");
   }
);

$("ul li:nth-child(2)").hover(
  function () {     
    $('#changeImage').attr('src', 'pic2.png');
  }
);

$("ul li:nth-child(3)").hover(
  function () {     
    $('#changeImage').attr('src', 'pic3.png');
  }
);

$("ul li:nth-child(4)").hover(
  function () {     
    $('#changeImage').attr('src', 'pic4.png');
  },
     function() {
    $(".content.one").css("display", "none");
   },
        function() {
    $(".content.two").css("display", "none");
   },
        function() {
    $(".content.three").css("display", "none");
   },
        function() {
    $(".content.four").css("display", "block");
   }
);

});
</script>

and the whole html 和整个html

<div id="wrapper">
<img src="clear.png">
<ul id="side-bar">
<li class="one"><a href="#"><img src="1-1.png"
onmouseover="this.src='1-2.png'"
onmouseout="this.src='1-1.png'"></a></li>
<li class="two"><a href="#"><img src="2-2.png"></a></li>
<li class="three"><a href="#"><img src="3-1.png"
onmouseover="this.src='3-2.png'"
onmouseout="this.src='3-1.png'"></a></li>
<li class="four"><a href="#"><img src="4-1.png"
onmouseover="this.src='4-2.png'"
onmouseout="this.src='4-1.png'"></a></li>
</ul>
<div id="image"><img id="changeImage" src="pic1.png"></div>
<div id="content-wrapper"><img src="content.png"></div>
<div class="content one"><p>content 1</p><a href="#" class="more">read more...</a></div>

<div class="content two"><p>content 2</p><a href="#" class="more">read more...</a></div>

<div class="content three"><p>content 3</p><a href="#" class="more">read more...</a></div>

<div class="content four"><p>content 4</p><a href="#" class="more">read more...</a></div>
</div>

You are using .hover() incorrectly. 您使用的.hover()错误。 Hover takes two functions as parameters: the function to execute on mouseover and the function to execute on mouseout. 悬停具有两个函数作为参数:在鼠标悬停时执行的功能和在鼠标悬停时执行的功能。 For example: 例如:

$(function() {

    $('#my-id').hover(

        // mouse over
        function() {
            $('#foo').show();
            $('#bar').addClass('baz');
        },

        // mouse out
        function() {
            $('#foo').hide();
            $('#bar').removeClass('baz');
        });
});

In your code you are passing four functions as parameters to the hover call bound to the element "ul li:nth-child(1)". 在代码中,您将四个函数作为参数传递给绑定到元素“ ul li:nth-​​child(1)”的悬停调用。 In all of your other hover event handlers you are only passing one function. 在所有其他悬停事件处理程序中,您仅传递一个函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM