2013-04-04 59 views
0

我在表中有textarea,並希望通過單擊來調整textarea的大小。我在jsFiddle中測試了腳本,但它不適用於我的項目。比我連接到腳本簡單的頁面,但沒有成功。 jQuery連接,另一個腳本工作,css連接。在jsFiddle中測試的jQuery腳本在firefox中不工作

下面的代碼:

$('textarea').click(function(){ 
    $('textarea').removeClass('active'); 
     $(this).addClass('textareastyle'); 
}); 

根據jsFiddle

與Firefox 20.0和jQuery 1.9.1測試(精縮)

+0

你在螢火蟲中看到什麼錯誤? – JoeCortopassi 2013-04-04 14:53:52

回答

1

寫您的代碼:DOM

$(document).ready(function() { 
     $('textarea').click(function(){ 
      $('textarea').removeClass('active'); 
      $(this).addClass('textareastyle'); 
    });  
}); 
+0

謝謝!現在它可以工作 – user2027175 2013-04-05 14:16:54

1

使用.focus().blur() ins的.click()

Fiddle

$('textarea').focus(function(){ 
    $(this).toggleClass('textareastyle'); 
}); 
$('textarea').blur(function(){ 
    $(this).toggleClass('textareastyle'); 
}); 
0

TEAD下面是從你的jsfiddle編譯後的代碼:

<html>                 
    <head>                 
     <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>   
     <script type="text/javascript">           
      $(document).ready(function() { 
        $('textarea').click(function(){ 
         $('textarea').removeClass('active'); 
         $(this).addClass('textareastyle'); 
       });  
      });       
     </script> 
     <style> 
      .textareastyle{ 
       width: 300px; 
       height:300px; 
       position: fixed; 
       margin-top:-2%; 
       margin-left:30%; 
       background-color:yellow; 
       color: black; 
      } 
     </style>          
    </head>                 
<body>                 
    <textarea class="dd"></textarea>          
</body>                 
</html> 

它可以在Firefox中,以及在鉻。

請記住在$(document).ready()方法中包裝類似這樣的代碼,以便在DOM加載時進行評估。

看看the beginner's tutorial以及API doc

相關問題