2011-09-21 57 views
0

我是新來的JQuery &我試圖做一些事情,我不知道我可以在JQuery做什麼。JQuery:轉換爲和從textarea元素

  • 我可以使用JQuery(或者甚至是本地Javascript)函數獲取HTML元素類型嗎?如果是這樣,該函數的名稱是什麼?
  • 我想更新/設置HTML元素類屬性。你會建議我使用普通的舊setAttribute()爲此還是應該使用JQuery函數?如果我應該使用JQuery函數,那麼函數的名稱是什麼?
  • 是否有一個JavaScript函數返回一個HTML元素類?它是this.getAttribute(「class」);?

更有經驗的JQuery的程序員:你會如何改善,試圖再次返回更改元素的textarea的這個jQuery HTML代碼(有些功能現在丟失):用於

<html> 
<head> 

    <script type="text/javascript" src="jquery-1.6.4.js"></script> 
    <script type="text/javascript"> 
    <!-- 
     var STATE = 0; 

     function Toggle() 
     { 
      if (STATE==1) { convertToUpdatable(); STATE = 0; } 
      else   { convertToStatic(); STATE = 1; } 
     } 

     function convertToUpdatable() 
     { 
      // Post: Convert all HTML elements (with the class 'updatable') to textarea HTML elements 
      //  and store their HTML element type in the class attribute 
      // EG: Before: <p class="updatable"/> Hello this is some text 1 </p> 
      //  After : <textarea class="updatable p"/> Hello this is some text 1 </textarea> 

      $(".updatable").each(function() 
       { 
        $(this).replaceWith("<textarea>"+$(this).text() +"</textarea>"); 
        // TODO store this elements type in class attribute 
        // The below is guessing that there are jquery functions getType() 
        $(this).setAttribute("class", $(this).getAttribute("class") + $(this).getType()); 
       }); 
     } 

     function convertToStatic() 
     { 
      // Post: Find all HTML elements (with the class 'updatable'), check to see if they are part of another class aswell 
      //  (which will be their original HTML element type) & convert the element back to that original HTML element type 

      $(".updatable").each(function() 
       { 
        // This uses javascript functions: how can I write this in JQuery 
        var type = this.getAttribute("class").replace("updatable","").replace(" ", ""); 

        if (type == "") { alert("Updatable element had no type defined in class attribute"); return; } 

        $(this).replaceWith(type +$(this).text() + type); 
        $(this).setAttribute("class", "updatable"); 
       }); 
     } 

    --> 
    </script> 
</head> 
<body> 

    <p class="updatable"/> Hello this is some text 1 </p> 
    <b class="updatable"/> Hello this is some text 2 </b> 
    <i class="updatable"/> Hello this is some text 3 </i> 

    <input id="MyButton" type="button" value="Click me!" onclick="Toggle();" /> 

</body> 
</html> 

回答

0

的Javascript:

your_element.className = 'some_class'; 
your_element.nodeName;