HTML input onfocus & onblur?

后端 未结 5 848
醉话见心
醉话见心 2021-02-06 00:49

ok, today I\'m making a helper HTML function. It looks like this:

function Input($name,$type,$lable,$value= null){
  if (isset($value)) {
    //if (this.value==\         


        
相关标签:
5条回答
  • 2021-02-06 01:26

    Looking at the source for this page shows the following

    <form id="search" action="/search" method="get"> 
      <div> 
         <input name="q" class="textbox" tabindex="1" 
         onfocus="if (this.value=='search') this.value = ''" 
         type="text" maxlength="80" size="28" value="search"> 
      </div> 
    
    0 讨论(0)
  • 2021-02-06 01:36

    Try this. Maybe it can help you:

    <form action="/search" method="get">
    <input type="text" name="q" value="Search..." onfocus="if (this.value == 'Search...') (this.value='')" onblur="if (this.value == '') (this.value='Search...')" />
    
    0 讨论(0)
  • 2021-02-06 01:38

    You want this

    <input ...
    onfocus="if (this.value==this.defaultValue) this.value = ''"
    onblur="if (this.value=='') this.value = this.defaultValue" />
    

    Update: Some newer browsers will do what you want simply by adding the placeholder attribute:

    <input placeholder="Please enter your name" />
    

    Here is the PHP according to your code

    echo <<<END
    <label for="$name">$lable</label>
    <input type="$type" name="$name" id="$name" value="$value" 
     onfocus="if (this.value==this.defaultValue) this.value = ''" 
     onblur="if (this.value=='') this.value = this.defaultValue"/>
    END;
    
    0 讨论(0)
  • 2021-02-06 01:49

    If I understand correctly, SO uses this line of HTML to do that effect.

    <input name="q" class="textbox" tabindex="1" onfocus="if (this.value=='search') this.value = ''" type="text" maxlength="80" size="28" value="search"> 
    

    Not related to your question, but you could and should replace your second if statement with an else statement.

    function Input($name,$type,$lable,$value= null){
      if (isset($value)) {
        //if (this.value=='search') this.value = ''
        echo '<label for="'. $name .'">'. $lable .'</label><input type="'.$type.'" name="'. $name .'" id="'. $name .'" value="'.$value.'" onfocus="if (this.value==\''.$value.'\') this.value = \'\' "/>';  
      }
      else {
        echo '<label for="'. $name .'">'. $lable .'</label><input type="'.$type.'" name="'. $name .'" id="'. $name .'" />'; 
      }
    }
    

    Because if isset($value) returns false then you know for a fact that it isn't set since it can only return one of two values, true or false.

    EDIT: Disregard this answer. It was not clear what the question was before the edit.

    0 讨论(0)
  • 2021-02-06 01:49

    Or this if you want it to change back to default no matter what text is entered...

    <input ... 
    onfocus="if (this.value==this.defaultValue) this.value = ''" 
    onblur="if (this.value!=this.defaultValue) this.value = this.defaultValue" />
    
    0 讨论(0)
提交回复
热议问题