首页 \ 问答 \ 使用jquery悬停时显示文本(display text when hovering with jquery)

使用jquery悬停时显示文本(display text when hovering with jquery)

我是jquery的新手,并且在将鼠标悬停在div上时尝试显示文本。 我现在有一个基本的html页面,带有几个正方形,其中一个正在单击按钮时旋转。 现在,在同一个方块(div2)中,我希望将鼠标悬停在此上时显示一些文本。

这是我的HTML

<!DOCTYPE html>
<html>
<head>
<LINK href="divtest.css" rel="stylesheet" type="text/css"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src=divtest.js></script>
</head>
<body>
<div id="border">
<div id="div1">
<div id="div2">
</div> 
</div>
</div>
<input type="button" value="button" onclick="rotate()" />

<script>
$("id=div2").hover(
function () {
    $(this).append($("<span> HOVERING!!!!! </span>"));
});
</script>

</body>
</html>

我如何确定我想要的文字“HOVERING !!!” 在div2上显示。

这是CSS

#border{
position:relative;
border-style:solid;
border-width:5px;
border-color:black;
width:1366;
height:768
}

#div1{
position:relative;
background-color: blue;
display:block;
height:500px;
width:500px;
left:350px;
}

#div2{
position:relative;
background-color: green;
display:block;
height:400px;
width:400px;
top:50px;
left:50px;
}

现在我的.js页面

var obj = "div2";
    var angle = 0;
    var interval = 100;
    increment = 5;

   function rotate() {
    $('#' + obj).css({
        '-moz-transform': 'rotate(' + angle + 'deg)',
        'msTransform': 'rotate(' + angle + 'deg)',
        '-webkit-transform': 'rotate(' + angle + 'deg)',
        '-o-transform': 'rotate(' + angle + 'deg)'
    });
        angle += increment;
        setTimeout(rotate, interval);
    }

I'm new to jquery and attempting to display text when hovering over a div. I have a basic html page right now with a couple squares, one of which rotates when a button is clicked. Now, in this same square (div2) I'd like for some text to be displayed when hovering over this.

Here's my HTML

<!DOCTYPE html>
<html>
<head>
<LINK href="divtest.css" rel="stylesheet" type="text/css"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src=divtest.js></script>
</head>
<body>
<div id="border">
<div id="div1">
<div id="div2">
</div> 
</div>
</div>
<input type="button" value="button" onclick="rotate()" />

<script>
$("id=div2").hover(
function () {
    $(this).append($("<span> HOVERING!!!!! </span>"));
});
</script>

</body>
</html>

How do I identify that I want the text "HOVERING!!!" to be displayed over div2.

Here's the CSS

#border{
position:relative;
border-style:solid;
border-width:5px;
border-color:black;
width:1366;
height:768
}

#div1{
position:relative;
background-color: blue;
display:block;
height:500px;
width:500px;
left:350px;
}

#div2{
position:relative;
background-color: green;
display:block;
height:400px;
width:400px;
top:50px;
left:50px;
}

And now for my .js page

var obj = "div2";
    var angle = 0;
    var interval = 100;
    increment = 5;

   function rotate() {
    $('#' + obj).css({
        '-moz-transform': 'rotate(' + angle + 'deg)',
        'msTransform': 'rotate(' + angle + 'deg)',
        '-webkit-transform': 'rotate(' + angle + 'deg)',
        '-o-transform': 'rotate(' + angle + 'deg)'
    });
        angle += increment;
        setTimeout(rotate, interval);
    }

原文:https://stackoverflow.com/questions/10432481
更新时间:2023-06-15 16:06

最满意答案

我过去处理这个问题的一种方法是利用一个简单的操作结果类。 基本上你所做的是将你的成功和失败包装在一个对象中,该对象包含操作的状态,它返回的任何数据,以及描述发生的事情的消息。 这是一个样本

public enum OperationStatus { Success, Failure, Pending }
public class OperationResult<T>
{

    public T Data { get; private set; }
    private OperationStatus opStatus;

    public string Status
    {
        get
        {
            return this.opStatus.ToString();
        }

        private set
        {
            var names = Enum.GetNames(typeof(OperationStatus));
            if (names.Contains(value))
            {
                this.opStatus = (OperationStatus)Enum.Parse(typeof(OperationStatus), value);
            }
            else
            {
                throw new Exception("Illegal Status Type");
            }
        }
    }

    public string Message { get; private set; }

    public bool IsSuccess
    {
        get
        {
            return this.Status == OperationStatus.Success.ToString();
        }
    }

    public OperationResult(OperationStatus status, string message, T data)
    {
        this.Data = data;
        this.Status = status.ToString();
        this.Message = message;
    }

    public static implicit operator bool(OperationResult<T> result)
    {
        return result.IsSuccess;
    }
}

我将向您推荐一篇博文,其中讨论了类似的方法响应技术http://www.appvnext.com/blog/2015/12/10/outcome-basics-part-i

使用这种技术将为您的提供商提供一种约定,以传达任何异常和成功。


One way I have handle this in the past is by leveraging a simple operation result class. Essentially what you do is wrap your success and failures in an object that holds the status of the operation, any data it returned, and maybe a message describing what happened. Here's a sample

public enum OperationStatus { Success, Failure, Pending }
public class OperationResult<T>
{

    public T Data { get; private set; }
    private OperationStatus opStatus;

    public string Status
    {
        get
        {
            return this.opStatus.ToString();
        }

        private set
        {
            var names = Enum.GetNames(typeof(OperationStatus));
            if (names.Contains(value))
            {
                this.opStatus = (OperationStatus)Enum.Parse(typeof(OperationStatus), value);
            }
            else
            {
                throw new Exception("Illegal Status Type");
            }
        }
    }

    public string Message { get; private set; }

    public bool IsSuccess
    {
        get
        {
            return this.Status == OperationStatus.Success.ToString();
        }
    }

    public OperationResult(OperationStatus status, string message, T data)
    {
        this.Data = data;
        this.Status = status.ToString();
        this.Message = message;
    }

    public static implicit operator bool(OperationResult<T> result)
    {
        return result.IsSuccess;
    }
}

I'll refer you to a blog post that discusses a similar technique for method responses http://www.appvnext.com/blog/2015/12/10/outcome-basics-part-i.

Using such a technique will provide a convention for your providers to communicate any exceptions and also successes.

相关问答

更多

相关文章

更多

最新问答

更多
  • Runnable上的NetworkOnMainThreadException(NetworkOnMainThreadException on Runnable)
  • C ++ 11 + SDL2 + Windows:多线程程序在任何输入事件后挂起(C++11 + SDL2 + Windows: Multithreaded program hangs after any input event)
  • AccessViolationException未处理[VB.Net] [Emgucv](AccessViolationException was unhandled [VB.Net] [Emgucv])
  • 计算时间和日期差异(Calculating Time and Date difference)
  • 以编程方式标签NSMutableAttributedString swift 4(Label NSMutableAttributedString programmatically swift 4)
  • C#对象和代码示例(C# objects and code examples)
  • 在python中是否有数学nCr函数?(Is there a math nCr function in python? [duplicate])
  • 检索R中列的最大值和第二个最大值的行名(Retrieve row names of maximum and second maximum values of a column in R)
  • 给定md5哈希时如何查找特定文件(How to find specific file when given md5 Hash)
  • Python字典因某些原因引发KeyError(Python Dictionary Throwing KeyError for Some Reason)
  • 如何让Joomla停止打开新标签中的每个链接?(How do I get Joomla to stop opening every link in a new tab?)
  • DNS服务器上的NS记录不匹配(Mismatched NS records at DNS server)
  • Python屏幕捕获错误(Python screen capture error)
  • 如何在帧集上放置div叠加?(How to put a div overlay over framesets?)
  • 页面刷新后是否可以保留表单(html)内容数据?(Is it possible to retain the form(html) content data after page refreshed?)
  • 使用iTeardownMyAppFrame和iStartMyAppInAFrame在OPA5测试中重新启动应用程序超时(Restart app within OPA5 test using iTeardownMyAppFrame and iStartMyAppInAFrame timed out)
  • 自动拆分文本内容到列(Automatically splitting text content into even columns)
  • 在r中的循环中将模型名称分配给gbm.step(assigning model names to gbm.step in loop in r)
  • 昆明哪里有电脑等级考试二级C培训?
  • C ++模板实例化,究竟是什么意思?(C++ template instantiation, what exactly does it mean?)
  • 帮助渲染来自fields_for的部分内容(Help to render a partial from fields_for)
  • 将url.action作为json对象返回mvc(return url.action as json object mvc)
  • 使用.BAT中的.application文件类型运行ac#Console App(Run a c# Console App with .application file type from a .BAT)
  • 将bindingRedirect添加到.Net标准库(Adding a bindingRedirect to a .Net Standard library)
  • Laravel版本升级会影响您的控制器吗?(Laravel version upgrade affects your controller?)
  • imaplib.error:命令SEARCH在状态AUTH中非法,只允许在SELECTED状态(imaplib.error: command SEARCH illegal in state AUTH, only allowed in states SELECTED)
  • 如何在eclipse debug impala前端
  • 如何通过Ajax API处理多个请求?(How to handle multiple requests through an Ajax API? [closed])
  • 使用Datetime索引来分析数据框数据(Using Datetime indexing to analyse dataframe data)
  • JS 实现一个菜单效果