使用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
最满意答案
我过去处理这个问题的一种方法是利用一个简单的操作结果类。 基本上你所做的是将你的成功和失败包装在一个对象中,该对象包含操作的状态,它返回的任何数据,以及描述发生的事情的消息。 这是一个样本
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.
相关问答
更多-
我过去处理这个问题的一种方法是利用一个简单的操作结果类。 基本上你所做的是将你的成功和失败包装在一个对象中,该对象包含操作的状态,它返回的任何数据,以及描述发生的事情的消息。 这是一个样本 public enum OperationStatus { Success, Failure, Pending } public class OperationResult
{ public T Data { get; private set; } private OperationStatus ... -
绝对不是 - 它应该被禁止(也就是说:作为用户改变它没有给你选项)。 这不是特定于您的库,但任何返回Map而不是TreeMap的代码都可以说。 ceilingEntry ,如果你返回一个Map ,那么用户不希望通过对参考的ceilingEntry来调用ceilingEntry (在TreeMap可用,但不在Map )。 通过它的Collectors.toXXX java-8做同样的事情。 例如Collectors.toList明确说: 返回的List的类型,可变性,可序列化或线程安全性无法保证。 这意味着他 ...
-
您需要关联的类型而不是类型参数: trait Producer { type Output; fn produce(&self) -> Self::Output; } trait Transformer { type Input; type Output; fn transform(&self, val: Self::Input) -> Self::Output; } trait Consumer { type Input; fn consume( ...
-
PhotoViewModel类包含两个名为Value属性,其中一个PropertyViewModel在PropertyViewModel定义,类型为object 。 另一个是在PhotoViewModel定义的,类型为PhotoValueViewModel (这基本上隐藏了其他属性,您应该从Visual Studio收到警告)。 在扩展方法中,您引用的是PropertyViewModel.Value ,它是object类型 这是因为在此方法中声明的TDestination类型可从PropertyViewMo ...
-
你需要模板方法模式 。 在FileTransfer类中创建一个doTransfer方法( final ),用于定义需要调用的方法的顺序。 使download和upload方法也是final以便它们不能被覆盖。 process方法是abstract方法; 使其protected因此不能从其他类中调用。 它不能public ,否则它可能会被其他一些课程无序地调用。 public final void doTransfer() { // protected methods since we want to ...
-
强制执行函数调用的顺序?(enforce order of function calls?)[2021-12-21]
实际上有两种特定的方式,取决于你是继承还是参数化。 如果你有继承,那就是Template Method模式: class Base { public: void doit() { this->do1(); this->do2(); } private: virtual void do1() = 0; virtual void do2() = 0; }; 如果你使用参数化,那就是Strategy模式: class Strategy { publ ... -
你不需要doWork声明中的
,因为你想使用在类级别声明的T - 你需要 public abstract void doWork(T result); 目前的声明与之相同 public abstract void doWork(A result); 它不一定和你班上的其他人一样。 You don't need the in the declaration of doWork, because you want to use the T that is declared at the ... -
类通过type上的数据描述符具有__name__属性: >>> Sub.__name__ 'Sub' >>> '__name__' in Sub.__dict__ False 它是一个数据描述符,因为它还拦截赋值以确保该值是一个字符串。 实际值存储在C结构的插槽中,描述符是该值的代理(因此在类上设置新值也不会向__dict__添加新条目): >>> Sub.__name__ = None Traceback (most recent call last): File "
", line 1 ... -
我们不希望该方法一直抛出异常(否则我们永远不会得到有意义的结果),我们说“这个方法可能抛出这个异常”。 如果没有意义,让子类不抛出异常就完全没问题了。 如果您的客户端代码知道它正在处理子类,那么它将不必处理异常,作为额外的奖励。 在实现中不声明throw子句来实现声明throw子句的接口方法也是完全没问题的。 We don't want the method to throw an exception all the time (otherwise we'll never get a meaningful ...
-
不能强制使用抽象类的基础构造函数到派生类中(Can't enforce the use of base constructor of an abstract class into derived class)[2023-05-10]
以下构造函数尝试在没有任何参数的情况下调用基类的构造函数。 public AddPackageMethod(IListlinks) { } 好吧,由于params关键字, 可以在没有任何参数的情况下调用基类的构造函数。 所以编译得很好。 public ParameterizedMethod(params Parameter[] parameters) { SetParameters(parameters); } 仅用于测试,如果删除params关键字,从而强制传递参数,则代码将无法 ...