0

I've a snag , here it is simple case

<input type="text" id="some" value="something">

and javascript

document.getElementById("some").onfocus = function(){
    this.style.opacity = 0.5;
};

It works just fine , but I want also that during this time (onfocus) mouse pointer be at the beginning of input field ( I mean to ignore default value , not to continue from "something") , any ideas ? thanks :))

P.S This is the just same like it is on facebook search, when focus search field and the default text become dim and the pointer moves at the beginning of input field

4

2 回答 2

1

你可以使用这个事件来做这样的onkeypress事情。授予不是最伟大的代码,但它应该让你开始。

HTML:

<input type="text" id="some" value="something">

​ Javascript:

i = 1;
document.getElementById("some").onkeypress= function(){    
    if(i === 1) {
        document.getElementById("some").value = '';
        i = 0;
    }
    this.style.color = 'black';
}

document.getElementById("some").onfocus = function(){
    this.style.color = 'gray';
};

​ JS 小提琴:http: //jsfiddle.net/wZAvn/

于 2012-07-10T20:57:37.787 回答
0

或类似的东西:

var some = document.getElementById("some"),
val = some.value; 
some.onfocus = function(){
    some.style.opacity = 0.5;
    some.value = (some.value == val )?"":some.value;        
};​

http://jsfiddle.net/w5Kv8/1/

于 2012-07-10T20:33:18.047 回答