2016-03-09 24 views
1

我似乎無法得到這個工作。我正在使用JQuery和這種新的。 (a){return J ...... 並且腳本不起作用。試圖設置一個字段使用jquery - 不起作用

<!DOCTYPE html> 
<html>  
<head> 

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> 

    <script type="text/javascript"> 

     $(document).ready(function() 
     {     
      $("input").click(function() 
      { 

       alert('before...' + $(this).text); 

       if ($(this).text == "Search ") 
       { 
        $(this).val("You have set me to value."); 
        $(this).css({"style.color":"#000000"}); 
        $(this).css({"fontStyle":"italic"}); 
       } 
       else 
       { 
        $(this).val("");      
       } 

       alert('after...' + $(this).text);      

      });   
     }); 

    </script> 

    <title>My page</title> 

</head> 

<body> 
    <div class="historysearch"> 
     <input id="textinput" type="text" value="Search "/>        
    </div> 
</body> 

回答

0

使用val()方法獲取的輸入值不.text

工作演示

$(document).ready(function() { 
 
    $("input").click(function() { 
 

 
    alert('before...' + $(this).val()); 
 

 
    if ($(this).val() == "Search") { 
 
     $(this).val("You have set me to value."); 
 
     $(this).css({ 
 
     "style.color": "#000000" 
 
     }); 
 
     $(this).css({ 
 
     "fontStyle": "italic" 
 
     }); 
 
    } else { 
 
     $(this).val(""); 
 
    } 
 

 
    alert('after...' + $(this).val()); 
 

 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="historysearch"> 
 
    <input id="textinput" type="text" value="Search " /> 
 
</div>

+0

非常感謝! – user3020047