函数参数不适用于.classList.add

时间:2019-09-18 12:11:10

标签: javascript

我已经创建了一个简单的函数来删除和添加类,但是我无法使其正常工作,下面是要测试的html,它需要工作,以便当鼠标进入'.dropdown'时#myDropdown将获得一个将类添加到“显示”中,并且当鼠标离开时,该类将被删除:

const dropDown = document.querySelector('.dropdown');
const dropDownList = dropDown.querySelector('#myDropdown');
const classToToggle = 'show';
dropDown.addEventListener("mouseenter", toggleClass(dropDownList, classToToggle));
dropDown.addEventListener("mouseleave", toggleClass(dropDownList, classToToggle));

function toggleClass(target, className) {
    if(target.classList.contains(className)) {
        target.classList.remove(className);
    } else {
        target.classList.add(className);
    }
}
<div class="dropdown">
        <a href="javascript:void(0)" class="dropbtn" style="outline: none;">Dropdown <i class="fas fa-chevron-down"></i></a>
        <div id="myDropdown" class="dropdown-content">
            <p>Item 1</p>
            <p>Item 2</p>
            <p>Item 3</p>
            <p>Item 4</p>
        </div>
    </div>

即使当我在console.log中记录“ className”和“ target”参数时,该函数也没有在“ target”中添加“ className”。

谢谢

1 个答案:

答案 0 :(得分:2)

Name: U.S. AbilityOne Commission Website: http://www.abilityone.gov Address: 1401 S. Clark Street Suite 715 Arlington, VA 22202-3259 Name: U.S. Access Board Website: http://www.access-board.gov/ Address: 1331 F St., NW Suite 1000 Washington, DC 20004-1111 Name: Administration for Children and Families Website: http://www.acf.hhs.gov/ Address: 330 C St., SW Washington, DC 20201 Name: Administration for Community Living Website: http://www.acl.gov Address: One Massachusetts Ave., NW Washington, DC 20201 Name: Administration for Native Americans Website: http://www.acf.hhs.gov/programs/ana/ Address: 2nd Floor, West Aerospace Center 370 L'Enfant Promenade, SW Washington, DC 20447-0002 Name: Administrative Conference of the United States Website: http://acus.gov/ Address: 1120 20th St., NW Suite 706 South Washington, DC 20036 Name: Administrative Office of the U.S. Courts Website: http://www.uscourts.gov/ Address: One Columbus Circle, NE Washington, DC 20544 ... 需要回调函数,而不是函数调用结果(在您的情况下会发生)。将您的函数包装在另一个函数中以解决该问题:

BEGIN
declare @au_in char( 11 )
declare @au_out char( 11 )
declare @rowcount char( 11 )

set rowcount 0
select jargonf1_sqladmin.StaffRegisterV2.signInTime,jargonf1_sqladmin.StaffRegisterV2.signOutTime from jargonf1_sqladmin.StaffRegisterV2

set rowcount 1


while @rowcount <> 0
BEGIN
select @au_in = signInTime from jargonf1_sqladmin.StaffRegisterV2
select @au_out = signOutTime from jargonf1_sqladmin.StaffRegisterV2
DECLARE @Sign_In datetime

DECLARE @Sign_Out datetime

DECLARE @WorkedHours DECIMAL(18,2)

DECLARE @hours varchar(30)

DECLARE @minutes varchar(2)

DECLARE @seconds varchar(30)

set @Sign_In =  @au_in

set @Sign_Out = @au_out

set @seconds = abs(datediff(second, @Sign_In, @Sign_Out))

set @hours = @seconds / 3600

set @minutes = (@seconds - (@hours * 3600)) / 60

set @seconds = (@seconds - (@hours * 3600) - (@minutes * 60))

set @WorkedHours = @hours + '.' + @minutes 

SELECT @WorkedHours AS WorkedHours
set @rowcount = 2 + 1
END

set rowcount 0

END