- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有两个列表,当我在一个列表中将鼠标悬停在 Tom 上时,我希望关于他的描述变为红色,因此我添加了一个悬停类,如下所示。我遇到的问题是我无法删除悬停状态,因为当我离开悬停状态时我变得不确定。我已经评论了到目前为止我尝试过的几件事。当我离开悬停时,有没有办法知道 id 的值或删除页面上任何名为 hover 的类?
<ul id="one-list">
<li id="u11">Tom</li>
<li id="u22">Jerry</li>
</ul>
<ul id="another-list">
<li id="a11">Tom is 22 years old</li>
<li id="a22">Jerry is 18 years old</li>
</ul>
$("#one-list li").hover(
function () {
var id = jQuery(this).attr('id') || '';
id = id.replace(/u/, '');
$('#another-list #a' + id ).addClass("hover");
}, function () {
//$('body').removeClass("hover");
//$('#another-list #a' + id ).removeClass("hover");
}
);
.hover {
color:red;
}
最佳答案
您收到该错误是因为 id
仅在第一个函数中定义。您需要在第二个函数中再次获取 id
,例如:
$("#one-list li").hover(
function () {
var id = jQuery(this).attr('id') || '';
id = id.replace(/u/, '');
$('#another-list #a' + id ).addClass("hover");
}, function () {
var id = jQuery(this).attr('id') || '';
id = id.replace(/u/, '');
$('#another-list #a' + id ).removeClass("hover");
}
);
或者更简单
$("#one-list li").hover(
function () {
var id = this.id.replace('u', '');
$('#another-list #a' + id ).addClass("hover");
}, function () {
var id = this.id.replace('u', '');
$('#another-list #a' + id ).removeClass("hover");
}
);
或者更好:
$("#one-list li").hover(
function () {
var id = this.id.replace('u', '');
$('#another-list #a' + id ).toggleClass("hover");
}
);
关于javascript - 悬停时 undefined variable ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20693248/
我只是有一个更琐碎的问题。 为什么undefined == undefined 返回true,而undefined >= undefined 为false? undefined 等于 undefine
用PHP 7.2编写套接字服务器。根据Firefox 60中的“网络”选项卡,服务器的一些HTTP响应的第一行随机变为undefined undefined undefined。因此,我尝试记录套接字
在 JavaScript 中这是真的: undefined == undefined 但这是错误的: undefined <= undefined 起初我以为<=运算符包含第一个,但我猜它试图将其转换
在回答这个问题 (Difference between [Object, Object] and Array(2)) 时,我在 JavaScript 数组中遇到了一些我以前不知道的东西(具有讽刺意味的
来自https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/of , Note: thi
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
当我添加 到我的 PrimeFaces Mobile 页面,然后我在服务器日志中收到以下警告 WARNING: JSF1064: Unable to find or serve resource, u
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我是一名优秀的程序员,十分优秀!