Javascript 如何在悬停到图像时显示消息

标签 javascript jquery image hover mouse

我可以使用以下代码显示图像警告。如果用户名或密码为空。

如果鼠标悬停在警告图像上,我想显示消息。

例如;

“用户名不能为空!”对于用户名和“密码不能为空!”密码。

在何处编辑以在鼠标悬停到图像时显示消息?

我的 Javascript 代码:

function LoginButonOnclick() {
  var data= {
  UserName: $('#UserNameTextBox').val(),
  Password: $('#PasswordTextBox').val(),
  };
  if (data.UserName== null) {
    $("#showwarning1").html('<img src="~/Image/warning.png">');
  }
  if (data.Password== null) {
    $("#showwarning2").html('<img src="~/Image/warning.png" />');
  }
}

我的 HTML 代码:

<input type="text" id="UsernameTextBox" name="UsernameTextBox"/>
<input type="text" id="PasswordTextBox" name="PasswordTextBox"/>
<input type="button" onclick="LoginButonOnclick()" value="Enter"/>

<div id="showwarning1"></div>
<div id="showwarning2"></div>

最佳答案

为什么要自己把事情复杂化......它是如此简单...... 使用 <img>title 属性显示鼠标悬停消息的标签!!

<img src="~/Image/warning.png" title="Password is empty!!"/>

关于Javascript 如何在悬停到图像时显示消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18910145/

相关文章:

jquery - Flot graph - 实时图表绘制中的线宽

image - 在 Qt 中如何给图像添加注释?

javascript - 无法抓取网页上类元素中的文本 - Chrome 扩展

javascript/select2 : keyboard shows when select is clicked

javascript - jquery 子级首先删除类

javascript - 我想知道如何在后面的代码中访问我的模态表单元素。另外我想知道如何在模态表单上应用 jquery 验证器

iphone - iPhone 应用程序中的图像出现在模拟器中,但在编译到设备时不会出现

php - 上传和调整图像大小时PNG黑色背景

javascript - 将特定文本添加到随机图像数组

javascript - 如何从 Google Picker API 获取多张选中的图片