如何在jquery上调用函数hover()

时间:2017-05-19 09:50:11

标签: jquery hover

我试图将函数调用到jquery hover() 首先,我创建函数objIn()以传递hover()的值并且它有效 在mousenter对象之后,我创建了运行第二个函数的标志,但是在将参数传递给函数testObj()时遇到了问题。

如何将值从jquery hover()传递给我创建的函数?



var objHover = $('.main ul li a, .content-wrap .content');

var flag = 'true';
var mainAnchor;

function objIn(mainAnchor) {
  var mainData = $('.main li a[data-hover= ' + mainAnchor + ']');
  var contentData = $('.content-wrap .content[data-hover= ' + mainAnchor + ']');

  function testObj(dataItem) {
    if (dataItem == 'true') {
      contentData.addClass('active');
      contentData.siblings().addClass('hidden');
      mainData.addClass('active');
      $(this).addClass('active');
    } else if (dataItem == 'false') {
      mainData.removeClass('active');
      contentData.removeClass('active');
      contentData.siblings().removeClass('hidden');
      $(this).removeClass('active');
    }
  }
}

objHover.hover(
  function() {
    flag = 'true';
    mainAnchor = $(this).data('hover');
    objIn(mainAnchor);
    testObj(flag);
  },
  function() {
    flag = 'false';
    mainAnchor = $(this).data('hover');
    objIn(mainAnchor);
    testObj(flag);
  });

.content-wrap{
  margin-top: 30px;
  padding-bottom: 60px;
  background-color: yellow;
  clear: both;
  overflow: hidden;
}

.content{
  padding: 80px;
  float: left;
  margin-right: 10px;
  cursor: pointer;
}

.main li a.active,
.content.active{
  border: 2px solid red;
}

.hidden{
  opacity: 0.7;
}
.content.hidden{
	background-color: gray;
}

.red {
  background-color: red;
}
.green {
  background-color: green;
}
.blue {
  background-color: blue;
}
.purple {
  background-color: purple;
}
.brown {
  background-color: brown;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="main">
  <ul>
    <li><a href="" data-hover="item1">item 1</a></li>
    <li><a href="" data-hover="item2">item 2</a></li>
    <li><a href="" data-hover="item3">item 3</a></li>
    <li><a href="" data-hover="item4">item 4</a></li>
    <li><a href="" data-hover="item5">item 5</a></li>
  </ul>
</div>

<div class="content-wrap">
  <div class="content red" data-hover="item1">content 1</div>
  <div class="content green" data-hover="item2">content 2</div>
  <div class="content blue" data-hover="item3">content 3</div>
  <div class="content purple" data-hover="item4">content 4</div>
  <div class="content brown" data-hover="item5">content 5</div>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

您已在其他函数中编写函数import java.util.ArrayList; import java.util.Scanner; import java.io.File; import java.io.PrintStream; public class Accountant { Member m = new Member("",0,0,""); President p = new President(); Double totalMoney = 0.0; public void pay(Double tempMoney) { System.out.println("Would you like to pay for your membership now? Y / N"); Scanner sYes = new Scanner(System.in); String yesS = sYes.next(); if(yesS.equalsIgnoreCase("Y")) { totalMoney = totalMoney + tempMoney; System.out.println("Total money " + totalMoney + " temp money " + tempMoney); } } } ,因此无法从外部访问。同时将testObjmainData定义为全局,以便在contentData函数中可用。 请把它放在外面,如下所示

testObj