jquery - 使用 QUnit 测试悬停 CSS 属性

标签 jquery css qunit

我想使用 QUnit 进行一个简单的测试,以检查元素悬停时的 CSS 属性。

假设我有一个 CSS 定义的链接

a:hover {text-decoration: none}

我试图让 QUnit 触发鼠标悬停事件,然后检查链接的 CSS 是什么,但它不起作用。但是,当我手动悬停链接时,测试给出了正确的结果。

这是 QUnit 代码

QUnit.test('hover test', function() {
    $('a').mouseover(function (){
        QUnit.ok($('a').css('text-decoration') == 'none');
    });

    $('a').mouseover();
}

最佳答案

你不能,因为它不是受信任的事件。

Events that are generated by the user agent, either as a result of user interaction, or as a direct result of changes to the DOM, are trusted by the user agent with privileges that are not afforded to events generated by script through the DocumentEvent.createEvent("Event") method, modified using the Event.initEvent() method, or dispatched via the EventTarget.dispatchEvent() method. The isTrusted attribute of trusted events has a value of true, while untrusted events have a isTrusted attribute value of false.

Most untrusted events should not trigger default actions, with the exception of click or DOMActivate events.

然而,您可以做的是根据您的 :hover 样式创建一个新类,然后在鼠标悬停/鼠标离开时切换该类。

关于jquery - 使用 QUnit 测试悬停 CSS 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31111613/

相关文章:

css - Arial 支持汉字吗?

javascript - Qunit 数据已删除

javascript - 在基于 Maven 的项目中自动化 JavaScript 测试

php - Bootstrap 下拉菜单不适用于包含 header php

php - preg_replace() 标签名称

javascript - 如何对编译后的代码进行单元测试(使用QUnit和Sinon)(闭包编译器高级编译)

javascript - 使用带有非标准选项的 jQuery 自动完成功能?

php - yii 验证电话号码的输入数组

jquery .index() 函数返回不正确

javascript - 如何从表的outerHTML 值创建列表对象?